public class Student String studentid String name String
一个超类 public class Student { String studentid; //学生编号 String name; //学生姓名 String speciality; //专业 public Student(String studentid, String name) { this. studentid = studentid; this. name = name; } //分配专业 public void assign. Speciality(String speciality){ this. speciality=speciality; } //定义该对象的字符串表示 public String to. String(){ return studentid+", "+name+", "+ speciality; } }
一个继承于Student的子类 public class Postgraduate extends Student{ String orientation; //增加了研究方向 public Postgraduate(String studentid, String name, : Postgraduate student String orientation) { super(studentid, name); //调用超类Student的构造函数 obj : Student this. orientation = orientation; studentid = 001 } name = Zhuhua public void change. Orientation(String nwe. Orientation){ //变更研究方向 speciality this. orientation=nwe. Orientation; Orientation = AI } public static void main(String[] args 0){ Postgraduate student=new Postgraduate("001", "Zhuhua", "AI"); student. assign. Speciality("Computer Science"); System. out. println(student); //默认执行超类的to. String(), } }
7. 3 Object类、抽象类、final类 Object public class Object{ public Object(){…} public final Class<? extends Object> get. Class(){…} public String to. String(){…} public int hash. Code(){…} public boolean equals(Object obj) {…} protected Object clone() throws Clone. Not. Supported. Exception{…} public final void notify(){…} public final void notify. All(){…} public final void wait(…) throws Interrupted. Exception{…} protected void finalize() throws Throwable{…} }
public abstract class Student{ String studentid; //学生编号 String name; System. out. println(student) String speciality; //定义该对象的字符串表示 public String to. String(){ return studentid + ", “ + name + ", “ + speciality; } }
public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Account)) return false; final Account other = (Account) obj; if (id == null) { if (other. get. Id() != null) return false; } else if (!id. equals(other. get. Id())) return false; return true; }
JAVA语言 漳州师院 许晴媛 4. 2 Object类(续) ——equals方法 的重写 l 在Bank. Account类中增加equals方法。 public boolean equals(Object x) { if (this. get. Class() != x. get. Class()) return false; Bank. Account b = (Bank. Account) x; return ((this. get. Owner. Name(). equals(b. get. Owner. Name())) &&(this. get. Account. Number() = = b. get. Account. Number()) &&(this. get. Balance() = = b. get. Balance())); }
JAVA语言 漳州师院 许晴媛 4. 2 Object类(续) ——例4_9 l equals方法的应用举例2 public class Apple { private String color; private boolean ripe; public Apple(String a. Color, boolean is. Ripe) { color = a. Color; ripe = is. Ripe; }
JAVA语言 漳州师院 许晴媛 4. 2 Object类(续) ——例4_9 public boolean equals(Object obj) { if (obj instanceof Apple) { Apple a = (Apple) obj; return (color. equals(a. get. Color()) && (ripe == a. get. Ripe())); } return false; } }
示例 Integer a=new Integer(5); Integer b=new Integer(5); 则下面的语句的真假是? a==b a. equals(b) ?false ? true
String str 1=new String("hello"); String str 2=new String("hello"); String str 3=str 1; //两个变量指向同一个字符串对象 false ?str 1==str 2 ?str 1. equals(str 2) true ?str 1==str 3 true ?str 1. equals(str 3) true ?"hello"=="hello“ true ?new String(“hello”)==new String(“hello”) ?false
7. 3. 2抽象类 如下面的类就是一个抽象类: public abstract class Geometric. Figure{ String color; String get. Color(){ Return this. color; } public abstract double get. Area(); }
7. 3. 2抽象类 或者 public abstract class geometric{ String color; String get. Color(){ Return this. color; } }
JAVA语言 漳州师院 许晴媛 抽象类和抽象方法例子 l 假设声明一个Shape类代表图形的概念,图形都具有求面积 get. Area() 的方法。 Shape Two. Dimensional. Shape Circle Triangle Rectangle Three. Dimensional. Shape Cube Sphere Tetrahedro n Square l 把Shape声明成抽象类,把get. Area() 声明成抽象方法。 然后由Shape派生出各具体子类 Circle, Triangle, Rectangle, Square, Cube, Sphere, Tetrahedron l 在这些子类中分别给出各自get. Area()方法的具体实现,即给出每 种具体图形计算面积的方法。 l
JAVA语言 漳州师院 许晴媛 抽象类和抽象方法例子 public abstract Shape { static final double PI=3. 14; public abstract double get. Area(); }
JAVA语言 漳州师院 许晴媛 抽象类和抽象方法例子 Shape s; s=new Circle(); s. get. Area(); s=new Triangle(); s. get. Area();
JAVA语言 抽象类和抽象方法例子 漳州师院 许晴媛 Shape s s=new Circle() 或 new Rectangle() 或 new … s. get. Area(); // 可以看成是所有图形求面积的一个通用模板 Shape s[]=new Shape[7]; S[0]=new Circle(); S[1]=new Triangle(); S[2]=new Rectangle (); S[3]=new Square(); S[4]=new Cube(); S[5]=new Sphere (); S[6]=new Tetrahedron (); for (int i=0; i<s. length; i++) System. out. println(s[i]. get. Area());
7. 3. 3 final 1、final类 可以用“final”声明一个类不可再被作为超类 如: public final class Final. Class{ … }
7. 4 接口 下面是声明接口的语法: [public] interface Interface. Name [extends Super. Interface 1 [, …]]{ 常量声明; type method. Name 1(…); type method. Name 2(…); }
public interface Comparable<T>{ int compare. To(T o); } public interface Bank. Service { int withdraw(Account account, int amount); int deposit(Account account, int amount); int find. Balance(String id); }
一个实现接口的类 //程序 7 -4:实现了Comparable接口的Account类 import java. util. Arrays; public class Account implements Comparable{ @Override public int compare. To(Object o) { int result=0; Account t=(Account)o; return this. id. compare. To(t. get. Id()); } }
7. 4. 3 用接口定义变量 List accounts = null; accounts = new Array. List(); accounts = new Linked. List ();
JAVA语言 漳州师院 许晴媛 抽象类和抽象方法例子 l 假设声明一个Shape类代表图形的概念,图形都具有求面积 get. Area() 的方法。 Shape Two. Dimensional. Shape Circle Triangle Rectangle Three. Dimensional. Shape Cube Sphere Tetrahedro n Square l 把Shape声明成抽象类,把get. Area() 声明成抽象方法。 然后由Shape派生出各具体子类 Circle, Triangle, Rectangle, Square, Cube, Sphere, Tetrahedron l 在这些子类中分别给出各自get. Area()方法的具体实现,即给出每 种具体图形计算面积的方法。 l
JAVA语言 漳州师院 许晴媛 动态绑定例子 public class Binding. Tester { public static void main(String[] args) l 说明 Øl编译的时候是无法知道s 运行结果 数组元素的具体类型到 Square. draw() 底是什么 Triangle. draw() { Shape[] s = new Shape[9]; int n; Circle. draw() for(int i = 0; i < s. length; i++) Ø 运行的时候才能确定究 Triangle. draw() { n = (int)(Math. random() * 3); 竟是什么类型,所以是 Triangle. draw() switch(n) 动态绑定 Circle. draw() { case 0: s[i] = new Circle(3) ; break; Square. draw() case 1: s[i] = new Square(4) ; break; Ø 在主方法的循环体中, Circle. draw() case 2: s[i] = new Rectangle(4, 3) ; 每次根据运行时产生的 Triangle. draw() } //switch 随机数生成指向一个 }//for Circle、Square或者 for(int i = 0; i < s. length; i++) s[i]. get. Area(); Rectangle的引用,所 }} 以每次运行都可能有不 同的结果
多态性的例子 class Animal { public void eat( ){ System. out. println("Eat something. "); } } class Cat extends Animal { public void eat( ) { System. out. println("Eat mouse. "); } } class App { public static void main(String[] args){ Animal a = new Animal(); Cat c = new Cat(); a. eat(); c. eat(); a = c; a. eat(); } }
课堂练习 public class A{ public void a 1(){ public void a 2(){ } } } public class B{ public void b 1(){ public void b 2(){ } } } public class C{ private A a=new A(); private B b=new B(); public void a 1(){ a. b 1(); } public void a 2(){ a. b 2(); } public void b 1(){ b. b 1(); }; public void b 2(){ b. b 2(); }; }
课堂练习 class Student{ String sid; public Student(String sid){this. sid=sid; } public static void main(String args[]){ Object o 1=(Object)new Student(“ 200701”); Object o 2=new Student(“ 200701”); Object o 3=new Object(); boolean r=(o 1. equals(o 2)? true: false); System. out. println("o 1. equals(o 2)"+r); r=(o 1==o 2? true: false); System. out. println("o 1==o 2"+r); r=(o 1==o 3? true: false); System. out. println("o 1==o 3"+r); r=(o 1. equals(o 3)? true: false); System. out. println("o 1. equals(o 3)"+r); } } 问题: u 程序运行后的输 出是什么? u 如果学号相同, 则两个Student对 象认为是同一个 学生(即equal方 法返回true),则 Student类如何修 改
课堂练习 分析程序的运行结果 class C 1{ static int j=0; public void method(int a){ j++; } } class Test extends C 1{ public int method(){ return j++; } public void result(){ method(j); System. out. println(j+method()); } public static void main(String args[]){ new Test(). result(); } }
课堂练习 下面程序编译运行的结果是什么 interface Action{ int i=10; void do. Action(){ System. out. println(“do anything!”) } } class Happy implements Action { public static void main(String args[]) { Happy h=new Happy(); int j; j = Action. i; j = Happy. i; j = h. i; } }
多态性的例子 class Animal { public void eat( ){ System. out. println("Eat something. "); } } class Cat extends Animal { public void eat( ) { System. out. println("Eat mouse. "); } } class App { public static void main(String[] args){ Animal a = new Animal(); Cat c = new Cat(); a. eat(); c. eat(); a = c; a. eat(); } }
- Slides: 77