J2SE之面向对象_方法的重写_super_多态_抽象类_Final_接口
方法的重写(overwrite/override)
class Person { private String name; private int age; public void setName(String name){this.name=name;} public void setAge(int age) {this.age=age;} public String getName(){return name;} public int getAge(){return age;} public String getInfo() { return "Name: "+ name + "\n" +"age: "+ age; } } class Student extends Person { private String school; public String getSchool() {return school;} public void setSchool(String school) {this.school =school;} public String getInfo() { return "Name: "+ getName() + "\nage: "+ getAge() + "\nschool: "+ school; } } public class TestOverWrite { public static void main(String arg[]){ Student student = new Student(); Person person = new Person(); person.setName("none"); person.setAge(1000); student.setName("John"); student.setAge(18); student.setSchool("SCH"); System.out.println(person.getInfo()); System.out.println(student.getInfo()); } }
super 关键字
内存分析
继承中的构造方法
class SuperClass { private int n; /* SuperClass() { System.out.println("SuperClass()"); } */ SuperClass(int n) { System.out.println("SuperClass(" + n + ")"); this.n = n; } } class SubClass extends SuperClass { private int n; SubClass(int n) { //super(); System.out.println("SubClass(" + n + ")"); this.n = n; } SubClass() { super(300); System.out.println("SubClass()"); } } public class TestSuperSub { public static void main(String arg[]) { //SubClass sc1 = new SubClass(); SubClass sc2 = new SubClass(400); } }
练习1
练习2
Object类
toString方法
public class TestToString { public static void main(String[] args){ Dog d = new Dog(); System.out.println("d:="+d); } } class Dog { public String toString(){ return "I'm a cool Dog!"; } }
equals方法
public class TestEquals { public static void main(String[] args){ Cat c1 = new Cat(1,2,3); Cat c2 = new Cat(1,2,3); System.out.println(c1 == c2); System.out.println(c1.equals(c2)); //重写之前同上面一样 } } class Cat { int color; int height, weight; public Cat(int color, int height, int weight){ this.color = color; this.height = height; this.weight = weight; } public boolean equals(Object obj){ if(obj == null) return false; else{ if(obj instanceof Cat){ Cat c = (Cat)obj; if(c.color == this.color && c.height == this.height && c.weight == this.weight){ return true; } } } return false; } }
对象转型(casting)
对象转型实例 I
父类引用指向子类对象,他看到的只是作为父类那部分所拥有的属性和方法,子类的那部分不可用。
对象转型实例 II
动态绑定和多态
abstract class Animal { private String name; Animal(String name) {this.name = name;} /* public void enjoy(){ System.out.println("叫声......"); } */ public abstract void enjoy(); } abstract class Cat extends Animal { private String eyesColor; Cat(String n,String c) {super(n); eyesColor = c;} /* public void enjoy() { System.out.println("猫叫声......"); } */ //public abstract void enjoy(); } class Dog extends Animal { private String furColor; Dog(String n,String c) {super(n); furColor = c;} public void enjoy() { System.out.println("狗叫声......"); } } class Bird extends Animal { Bird() { super("bird"); } public void enjoy() { System.out.println("鸟叫声......"); } } class Lady { private String name; private Animal pet; Lady(String name,Animal pet) { this.name = name; this.pet = pet; } public void myPetEnjoy(){pet.enjoy();} } public class Test { public static void main(String args[]){ Cat c = new Cat("catname","blue"); Dog d = new Dog("dogname","black"); Bird b = new Bird(); //Lady l1 = new Lady("l1",c); Lady l2 = new Lady("l2",d); Lady l3 = new Lady("l3",b); //l1.myPetEnjoy(); l2.myPetEnjoy(); l3.myPetEnjoy(); } }
多态存在的必要条件
1:要有继承,
2:要有重写,
3:父类引用指向子类对象。
抽象类
Final关键字
public class TestFinal { public static void main(String[] args) { T t = new T(); //t.i = 8; } } final class T { final int i = 8; public final void m() { //j = 9; } } class TT extends T { }
接口
接口特性
接口举例 1
接口举例 2
内存图
public interface Valuable { public double getMoney(); } interface Protectable { public void beProtected(); } interface A extends Protectable { void m(); void getMoney(); } abstract class Animal { private String name; abstract void enjoy(); } class GoldenMonkey extends Animal implements Valuable, Protectable { public double getMoney() { return 10000; } public void beProtected() { System.out.println("live in the room"); } public void enjoy() { } public void test() { Valuable v = new GoldenMonkey(); v.getMoney(); Protectable p = (Protectable)v; p.beProtected(); } } class Hen implements A { public void m() {} public void beProtected() {} public double getMoney() { return 1.0; } public void getMoney() {} }
浙公网安备 33010602011771号