Java 04 | 类 class - 02 类与实例

Class 类的方法和变量细分为静态的和非静态的. 静态就是可以被类调用,所以静态方法/变量也称之为类方法/变量;非静态只能由实例调用,所以也称之为实例方法/变量。 静态变量 类变量 Class Variables 有static声明(静态变量). 静态变量一般是类本身固有的属性, 被该类所有实例共享。例如,我们可能需要用狗类的另一种生物学的统称“犬科”来作为类的说明, 这个时候可以用public static String binomen = "犬科";,这个变量理论上是由类来访问的。静态方法也类似. 以下代码定义了一个类来模拟狗,包含一个类变量作为这个类的说明,一个类方法用于发出叫声: public class Dog { public static String instruction = "狗类实例"; //类变量, 说明 public static void makeNoise() { System.out.println("汪!"); } } 这里没有定义main(), 在这种情况下如何直接运行这个类(java Dog), 程序是会报错的 错误: 在类 Dog 中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application. 你可以选择在里面添加一个main()方法. 但这次我们选择不定义具体的main(). 具体要如何运行, 我们可以另写一个类定义一个main()方法来调用这个类. public class DogLauncher { public static void main(String[] args) { Dog....

2016-12-24 · 2 min · Cong Chan

Java 04 | 类 class - 01 变量和方法

Class Java的语法是为了更容易地模拟真实世界而设计的. 比如用程序实现一只狗, 可以用定义一个类class来描述它. 类class里面包括变量Variable,方法method(可以理解为Python的函数function)。变量可以储存数据,方法可以处理数据。变量必须在类中声明(即不能离开类独立存在),不像Python或Matlab这样的语言可以在运行时添加新的变量。 构造对象的过程: 声明(declaration)引用变量: Dog smalldog; 创建对象:实例化 new Dog(20), 如果没有把它作为值赋给一个类声明变量, 那么这个实例化的值会被垃圾回收. 连接对象和引用:赋值对象给引用Dog smalldog = new Dog(5) 创建对象这一步,调用了Dog(), 不是普通的方法, 而是类的构造函数 Constructors. 构造函数 构造函数在初始化一个对象时执行, 构造函数与类名同名且没有返回类型, 而且可以带参数: /** 注意:构造函数与class类同名 但没有返回类型 */ public Dog(int w) { weight = w; } 然后在DogLauncher里实例化一只狗时, 直接Dog d = new Dog(20);即可. 在以上代码的基础上, 后续当我们想使用new和参数创建一只狗时,可以随时调用public Dog(int w)构造函数。对于熟悉Python的人来说,你可以理解java的构造函数为Python的__init__。 Java可以有与类同名的方法,只是要指明返回类型。 构造函数无法被继承 如果类有一个以上的构造函数,则参数一定要不一样,包括参数顺序和类型 构造函数链 执行new指令会启动构造函数的连锁反应(Constructor Chaining), 首先会执行其父类的构造函数, 依此类推连锁反应到Object类为止. 就算是抽象类, 也会有构造函数, 虽然不能被直接实例化, 但也会被唤醒. 理论上,每个类的构造函数需要先调用其父类的构造函数super(),依次入栈 public class Duck extends Animal { int size; public Duck(int newSize) { super(); // 调用父类构造函数, 且必须是在函数中的第一行 size = newSize; } } 如果明确写了super();, 则必须位于构造函数第一行....

2016-12-23 · 1 min · Cong Chan

Java 03 | 代码风格 注释 Javadoc

代码风格与注释 努力保持代码可读性。良好的编码风格的一些最重要的特点是: 一致的风格(间距,变量命名,缩进风格等) 大小(线不太宽,源文件不要太长) 描述性命名(变量,函数,类),例如变量或函数名称为年份或getUserName而不是x或f。让代码本身提供可解读性。 避免重复的代码:若有两个重要的代码块及其相似,应该想办法合并。 适当的评论, 使其他读者也能轻松理解你的代码 行注释: //分隔符开头行被当做注释。 Block(又名多行注释)注释: /*, */ , 但我们更推荐javadoc形式的注释。 Javadoc Javadoc: / **,*/, 可以(但不总是)包含描述性标签。 借助javadoc工具可以生成HTML格式的API文档。 第一段是方法的描述。描述下面是不同的描述性标签, 比如参数 @param, 返回值 @return, 可能抛出的任何异常 @throws /** * @author 名字,邮箱<address @ example.com> * @version 1.6 版本 * @param * @return */ public class Test { // class body }

2016-12-21 · 1 min · Cong Chan

Java 02 | 语法基础

Java基本语法 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } } 上面的程序由一个类声明组成,该声明使用关键字public class声明。 Java所有的代码都应该包含在class里面。 真正负责运行的代码,是一个名为main的method,它声明为public static void main(String[] args)。 public:公共的,大部分方法都是以这个关键字开始的,后面会进一步解释。 static:这是一个静态方法,不与任何特定的实例关联,后面会解释。 void:它没有返回类型。 main:这是方法的名称。 String [] args:这是传递给main方法的参数。 使用大括号{ }来表示一段代码的开始和结束。 声明必须以分号结尾 静态分类 Static Typing 程序语言静态与动态的分类,可以参考oracle的说明文件,它解释了动态和静态类型之间的区别, 帮助你理解由程序的错误提示信息。 两个主要区别: 1. 动态类型语言在运行时执行类型检查,而静态类型语言在编译时执行类型检查。这意味如果以静态类型语言(如Java)编写的脚本包含错误,则在编译错误之前将无法编译. 而用动态类型语言编写的脚本可以编译,即使它们包含会阻止脚本正常运行(如果有的话)的错误。 2. 静态类型语言要求你在使用它们之前声明变量的数据类型,而动态类型语言则不需要。 考虑以下两个代码示例: // Java int num; num = 5; # Python num = 5 这两段代码都创建一个名为num的变量并赋值为5. 不同之处在于Java需要将num的数据类型明确定义为int。因为Java是静态类型的,因此它期望变量在被赋值之前被声明。 Python是动态类型的,不需要定义类型, Python根据变量的值确定其数据类型。动态类型语言更加灵活,在编写脚本时可以节省时间和空间。但是,这可能会导致运行时出现问题。例如: # python number = 5 numbr = (number + 15) / 2 #注意错字 上面的代码本应创建一个值为5的可变数字,然后将其加上15并除以2以得到10....

2016-12-20 · 1 min · Cong Chan

Java 01 | 安装

Hello World 参考了伯克利 Josh Hug 的 cs61b spring 2017 和 cs61b spring 2018. Lab, homework 和 project 代码实现参考 https://github.com/ShootingSpace/cs61b-data-structures. Java安装与配置 安装Java,前往Oracle下载java sdk,我用的是Java SE 8u151/ 8u152 版本。安装sdk时会同时安装sdr。 Windows系统配置: 推荐安装git bash, 一切按照默认安装就好. 更新系统环境变量: 直接在运行中搜索Environment Variables, 选择编辑系统环境变量, 在弹出的框中选择高级->环境变量, 在弹出的框中系统变量里面 新建变量: 变量名 = JAVA_HOME, 变量值 = 你的jdk路径,如C:\Program Files\Java\jdk1.8.0_151 编辑Path: 在前面加入%JAVA_HOME%\bin;%PYTHON_HOME%;(请注意,不能有空格.) OS X系统配置: 安装Homebrew,一个非常好用的包管理工具。要安装,请在terminal终端输入ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"(注意:在此过程中,可能会提示输入密码。当输入密码时,终端上不会显示任何内容,但计算机还是会记录你的密码的。这是一个安全措施, 让其他人在屏幕上看不到你的密码。只需输入您的密码,然后按回车。) 然后,通过输入以下命令来检查brew系统是否正常工作brew doctor. 如果遇到警告,要求下载命令行工具,则需要执行此操作。请参考这个StackOverflow。 安装git:输入brew install git 安装并配置好java后,测试是否成功: 随便在你喜欢的文件夹里新建一个java文件HelloWorld.java public class HelloWorld { public static void main(String[] args) { System....

2016-12-18 · 1 min · Cong Chan

Inf Course Note - Accelerated Natural Language Processing

爱丁堡大学信息学院课程笔记 Accelerated Natural Language Processing, Informatics, University of Edinburgh References: Accelerated natural language processing ANLP revision guide Lecture Slides from the Stanford Coursera course Natural Language Processing, by Dan Jurafsky and Christopher Manning 概率模型 Probability Model 概率模型是随机现象的数学表示,由样本空间,样本空间内的事件以及与每个事件相关的概率定义。目标是模拟给一个事件发生的概率 估算概率(Probability Estimation)一般使用最大似然估计(MLE,相关频率):$$p(x_i) = \frac{Count(x_i)}{\sum_{i=0}^nCount(x_i)}$$ 平滑Smoothing 一般用于处理0概率的问题,比如在训练集中看不到, 但出现在测试集中的词。 Language modeling To compute the probability of sentence /sequence of words $P(w_1, w_2, w_3…)$, or to predict upcomming words $P(w|w_1, w_2, w_3…)$… a language model is also a probability model....

31 min · Cong Chan

Inf Course Note - Natural Language Understanding

爱丁堡大学信息学院课程笔记 Natural Language Understanding, Informatics, University of Edinburgh References: Natural language understanding CS224n: Natural Language Processing with Deep Learning Lecture Slides from the Stanford Coursera course Natural Language Processing, by Dan Jurafsky and Christopher Manning Meaning representations 意思的表达有很多方法。一种有效的表示单词的含义的方法是 distributional semantic. Semantics (from Ancient Greek: σημαντικός sēmantikos, “significant”) is the linguistic and philosophical study of meaning, in language, programming languages, formal logics, and semiotics. 语义学 Semantics 在语言学中的研究目的在于找出语义表达的规律性、内在解释、不同语言在语义表达方面的个性以及共性;与计算机科学相关的语义学研究在于机器对自然语言的理解。 Tradition solution of usable meaning in a computer: Use e....

28 min · Cong Chan

Inf Course Note - Parallel Programming Language and Systems

爱丁堡大学信息学院课程笔记 Parallel Programming Language and Systems, Informatics, University of Edinburgh Reference: http://www.inf.ed.ac.uk/teaching/courses/ppls/ CMU 15213: Introduction to Computer Systems (ICS) Computer Systems: A Programmer’s Perspective A Comprehensive MPI Tutorial Resource A chapter on MPI from Ian Foster’s online Book Designing and Building Parallel Programs Introduction to parallel computer architecture Covering some of the nasty issues presented by the shared memory model, including weak consistency models and false sharing in the cache, and some architectural issues for the multicomputer model....

63 min · Cong Chan

Inf Course Note - Software Architecture, Process, and Management

爱丁堡大学信息学院课程笔记 Software Architecture, Process, and Management, Informatics, University of Edinburgh Reference: microsoft IBM Software Architecture in Practice (3rd edition), Bass, Clements, and Kazman What is Software Architecture? Software architecture is often described as the organization or structure of a system, where the system represents a collection of components that accomplish a specific function or set of functions. grouping components into areas of concern (layers): For example, the UI, business processing, and data access....

45 min · Cong Chan

Inf Course Note - Software Testing

爱丁堡大学信息学院课程笔记 Software Testing, Informatics, University of Edinburgh Reference: http://www.inf.ed.ac.uk/teaching/courses/st/2017-18/index.html Pezze and Young, Software Testing and Analysis: Process, Principles and Techniques, Wiley, 2007. Why Software Testing? 1, 软件的漏洞, 错误和失效 Software Faults, Errors & Failures The problem start with Faults, Fault(BUG): latent error, mistakes in programming. e.g add(x, y) = x * y. With the Faults in programs, if and only if executing add(x, y) = x * y, the fault being activated, and generate an Errors....

49 min · Cong Chan