package Test;
public class Book {
long id;
String name;
double price;
}
package Test;
public class BookDemo {
public static void main(String[] args) {
Book book = new Book();
book.name = "Java基础";
System.out.println(book.name);
}
}
public Book(long id, String name, double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public Book() {
super();
}
public void read() {
System.out.println("读万卷书,行万里路");
}
package Test;
public class Question {
int id;
String text;
String[] options={};
public boolean check(String[] answers) {
return false;
}
public void print() {
System.out.println(id+"."+text);
for (int i = 0; i < options.length; i++) {
System.out.println(" "+options[i]);
}
System.out.println();
}
}
package Test;
public class SingleQuestion extends Question {
String answer;
public SingleQuestion(String answer, int id, String text, String[] options) {
this.id = id;
this.text = text;
this.options = options;
this.answer = answer;
}
@Override
public boolean check(String[] answers) {
if (answers == null || answers.length != 1) {
return false;
}
return this.answer.equals(answers[0]);
}
}
this是对当前对象的引用,是运行期间当前对象本身
可以使用this明确的访问当前对象的属性或方法,类似于“我”
this()可以调用本类的其他构造器,可以使用构造器的重用简化代码的实现
this()必须写在构造器的第一行
public class Point {
int x;
int y;
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public double distance() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
public double distance(int x, int y) {
return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y));
}
public double distance(Point other) {
return this.distance(other.x, other.y);
}
}
class Tiger {
int age;
int hight;
public Tiger() {
print();
}
public void print() {
System.out.println("I'am a " + age + "岁 " + hight + "尺高 tiger!");
}
}
public class JavanTiger extends Tiger {
public JavanTiger() {
super();
}
public static void main(String[] args) {
new JavanTiger();
}
}
1.final修饰的类,不能再被继承
Java中的string是final类,不能被继承
Math是final类,不能被继承
Integer、Long、Character等包装类是final类,不能被继承
在实际项目开发中,原则上不允许使用final类
注:违背动态代理技术
2.final修饰的方法,不能再被覆盖
3.final修饰的变量,初始化以后不允许再修改了
4.final static Java使用final static修饰的变量作为常量

/**
* 抽象方法,只有行为的概念,没有具体的行为实现
* 1.使用abstract关键字修饰,并且没有方法体
* 2.包含抽象方法的类,就一定是抽象类
* 3.抽象类不能直接创建实例,可以定义运用变量
* 4.抽象类只能被继承,一个具体类继承一个抽象类,必须实现所有抽象方法
* 5.抽象方法和抽象类是非常适合作为系统的分析和设计的工具
*/
public abstract class Shape {
protected Point location;
/**
* 抽象方法,图像可以计算面积,具体计算过程不清楚(抽象的)
* @return double
*/
public abstract double area();
public boolean contains(Point p) {
return contains(p.x, p.y);
}
public abstract boolean contains(int x, int y);
}
/**
* 圆是具体类,继承图形就必须实现图形中的所有抽象方法
*/
public double area() {
return Math.PI * r * r;
}
public boolean contains(Point p) {
return this.center.distance(p) <= r;
}
public boolean contains(int x, int y) {
return this.center.distance(x, y) <= r;
}
/**
* 接口:全部的方法都是抽象方法,所有的属性都是常量
* 1.接口是特殊的抽象类
* 2.接口用来表示纯抽象概念,没有任何具体的方法和属性
* 3.接口不能实例化,可以定义变量
* 4.接口变量可以引用具体实现类的实例
* 5.接口只能被实现(继承),一个具体类实现接口,必须使用全部的实现方法
* 6.接口之间可以继承(implements)
* 7.一个具体类可以实现多个接口,实现多继承现象
* 8.接口中的属性,默认是常量 pubic static final
* 9.接口中的方法一定是pubic abstract
* 10.实现一个接口,使用关键字implements,实现实际上是一种继承关系
*/
public interface Car {
void run();
void stop();
}
public interface Product {
double getPrice();
}
public class QQ implements Car, Product {
public void run() {
System.out.println("启动出发");
}
public void stop() {
System.out.println("刹车");
}
public double getPrice() {
return 30000;
}
}
public class QQDemo {
public static void main(String[] args) {
QQ qq = new QQ();
Product p = qq;
System.out.println(p.getPrice());
Car car = qq;
car.run();
car.stop();
Car c = (Car)p;
c.run();
c.stop();
}
}
public class Test {
public static void main(String[] args) {
Foo f = new Foo();
System.out.println(f);
System.out.println(f.toString());
}
}
class Foo {}
com.thx.test.Foo@435c41b
com.thx.test.Foo@435c41b
比较对象分为俩种:“引用相等”“对象相等”
equals在Object中声明,默认的比较原则是:比较引用
==用于比较引用和比较基本数据类型时具有不同的功能:
比较基本数据类型,如果两个值相同,则结果为true
而在比较引用时,如果引用指向内存中的同一对象,结果为true
public boolean equals(Object obj)
其比较规则为:当参数obj引用的对象与当前对象为同一个对象时,就返回true,否则返回false.
hashCode 的常规协定是:
1.在 Java 应用程序执行期间,在同一对象上多次调用 hashCode 方法时,
必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有被修改。
从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。
如果根据 equals(Object) 方法,两个对象是相等的,
那么在两个对象中的每个对象上调用 hashCode 方法都必须生成相同的整数结果。
从一定角度来看,封装和继承几乎都是为多态而准备的。
多态的定义:指允许不同类的对象对同一消息做出响应。
即同一消息可以根据发送对象的不同而采用多种不同的行为方式。(发送消息就是函数调用)
实现多态的技术称为:动态绑定(dynamic binding),
是指在执行期间判断所引用对象的实际类型,
根据其实际的类型调用其相应的方法。
现实中,关于多态的例子不胜枚举。
比方说按下 F1 键这个动作,如果当前在 Flash 界面下弹出的就是 AS 3 的帮助文档;
如果当前在 Word 下弹出的就是 Word 帮助;在 Windows 下弹出的就是 Windows 帮助和支持。
同一个事件发生在不同的对象上会产生不同的结果。
1.可替换性(substitutability)。多态对已存在代码具有可替换性。
例如,多态对圆Circle类工作,对其他任何圆形几何体,如圆环,也同样工作。
2.可扩充性(extensibility)。多态对代码具有可扩充性。
增加新的子类不影响已存在类的多态性、继承性,以及其他特性的运行和操作。
3.接口性(interface-ability)。多态是超类通过方法签名,
向子类提供了一个共同接口,由子类来完善或者覆盖它而实现的。
4.灵活性(flexibility)。它在应用中体现了灵活多样的操作,提高了使用效率。
5.简化性(simplicity)。多态简化对应用软件的代码编写和修改过程,
尤其在处理大量对象的运算和操作时,这个特点尤为突出和重要。