1.对象转型(casting)
一个基类的引用不可以访问其子类对象新增加的成员(属性和方法)
可以使用引用变量
子类的对象可以当作基类的对象来使用称作向上转型(upcasting),反之成为向下转型(downcasting)
public class Animal { public String name; Animal(String name) { this.name = name; }}public class Dog extends Animal{ public String furColor; Dog(String n,String furColor) { super(n); this.furColor = furColor; }}public class Cat extends Animal{ public String eyeColor; Cat(String name,String eyeColor) { super(name); this.eyeColor = eyeColor; }}public class TestCasting { public static void main (String agrs[]){ Animal a =new Animal("coco"); Dog d = new Dog("bigblack","black"); Cat c = new Cat("smallblue","blue"); System.out.PRintln(a instanceof Animal); System.out.println(d instanceof Animal); System.out.println(c instanceof Animal); System.out.println(a instanceof Cat); a = new Dog("bigyellow","yellow"); System.out.println(a.name); System.out.println(a instanceof Animal); System.out.println(a instanceof Dog); Dog d1 = (Dog)a; System.out.println(d1.furColor); }}结果是:true
true
true
false
bigyellow
true
true
yellow
针对a = new Dog("bigyellow","yellow");的解释:
大框为Animal对象a
针对Dog d1 = (Dog)a;的解释:
public class TestCasting { public static void main (String agrs[]){ TestCasting test = new TestCasting(); Animal a =new Animal("coco"); Dog d = new Dog("bigblack","black"); Cat c = new Cat("smallblue","blue"); test.f(a);test.f(d);test.f(c); //一个基类的引用类型变量可以"指向"其子类的对象 } public void f(Animal a){ System.out.println("name:" +a.name); if(a instanceof Dog){ Dog dog =(Dog)a; System.out.println("furColor:"+dog.furColor); } else if(a instanceof Cat){ Cat cat =(Cat)a; System.out.println("eyeColor:"+cat.eyeColor); } }}结果是:name:coconame:bigblackfurColor:blackname:smallblueeyeColor:blue
2.动态绑定和多态
动态绑定是指“在执行期间”(而不是编译期间)判断所用对象的实际类型,根据其实际的类型调用其相应的方法。也就是说父类中的方法,在子类中进行了重写,在编译中调用了父类的方法,但是在实际的执行过程中要看,你实际new出来的对象是那个子类或者是父类,来执行相关类中的方法。
多态的条件:
摩托车和卡车继承自car,继承重写后,测试类时
car mt = new 摩托车(); 1.要有继承
2.要有重写
3.父类引用指向子类对象 public class Animal { private String name; Animal(String name) { this.name = name; } public void enjoy() { System.out.println("叫声......"); }}public class Dog extends Animal { private String furColor; Dog(String n, String c) { super(n); furColor = c; } public void enjoy() { System.out.println("狗叫声......"); }}public class Cat extends Animal { private String eyesColor; Cat(String n, String c) { super(n); eyesColor = c; } public void enjoy() { System.out.println("猫叫声......"); }}public class Bird extends Animal{ Bird(){ super("bird"); } public void enjoy() { System.out.println("鸟叫声......"); }}public 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); //new出来的Cat对象,执行时指向Cat的enjoy方法 Lady l2 = new Lady("l2", d); Lady l3 = new Lady("l3", b); l1.myPetEnjoy(); l2.myPetEnjoy(); l3.myPetEnjoy(); }}结果是:猫叫声......狗叫声......鸟叫声......
内存分析如下:
3.抽象类
用abstract关键字来修饰一个类时,这个类叫做抽象类;用abstract来修饰一个方法时,该方法叫做抽象方法。
含有抽象方法的类必须被声明为抽象类,抽象类必须被继承,抽象方法必须被重写。
抽象类不能被实例化。
抽象方法只需声明,而不需要实现。
有方法只需要定义不需要实现时,使用抽象,即是这个方法需要重写时
对上述的程序作如下的修改来说明抽象类:
public abstract class Animal { private String name; Animal(String name) { this.name = name; } public abstract void enjoy();}public class Dog extends Animal { private String furColor; Dog(String n, String c) { super(n); furColor = c; } public void enjoy() { System.out.println("狗叫声......"); }}public abstract class Cat extends Animal { private String eyesColor; Cat(String n, String c) { super(n); eyesColor = c; } public abstract void enjoy();}public class Bird extends Animal{ Bird(){ super("bird"); } public void enjoy() { System.out.println("鸟叫声......"); }}public 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 = newCat("catname","blue"); //由于Cat被定义为抽象类,所以不能被实例化 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(); }}结果是:狗叫声......鸟叫声......4.final关键字
final的变量的值不能够被改变(final的成员变量,final的局部变量(形参))
final的方法不能够被重写,final的类不能被继承
public void m(final int j){ j =9;}
不允许这样,这样定义说明在m方法内部不允许改变 j的值 5.接口
接口(interface)是抽象方法和常量值的定义集合
从本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,而没有变量和方法的实现
接口中声明的属性默认是public static final的,也只能是public static final的
接口中只能定义抽象方法,而且这些方法默认是public的,也只能是public的
接口可以继承其他的接口,并添加新的属性和抽象方法
多个无关的类可以实现同一个接口,一个类可以实现多个无关的接口
与继承关系类似,接口与实现类之间存在多态性
定义java类的语法格式:
权限修饰符
class 名字 [extends 父类][implements 接口列表]{ }
public interface Singer { public void sing(); public void sleep();}public interface Painter { public void paint(); public void eat(); public void setName(String i);}public class Student implements Singer { private String name; Student(String name) { this.name = name; } public void study() { System.out.println("studying"); } public void sing() { System.out.println(this.name + " want to sing!"); } public void sleep() { System.out.println(this.name + " want tosleep more!"); }}public class Teacher implements Singer,Painter{ private String name; public String getString() { return name; } Teacher(String name) { this.name = name; } public void setName(String name) { this.name = name; } public void teacher() { System.out.println("teachering"); } public void sing() { System.out.println(this.name + "want to sing!"); } public void sleep() { System.out.println(this.name + "want tosleep more!"); } public void paint() { System.out.println(this.name + "like painting!"); } public void eat() { System.out.println(this.name + "like eating!"); }}public class TestInterface { public static void main(String agrs[]) { Singer s1 = new Student("coco"); s1.sing(); s1.sleep(); Singer s2 = new Teacher("mimi"); s2.sing(); s2.sleep(); Painter p1 = (Painter) s2; p1.setName("nini"); p1.paint(); p1.eat(); }}结果是:
coco want to sing!
coco want tosleep more!mimi want to sing!mimi want tosleep more!nini like painting!nini like eating!
另外的,在main方法里添加一个
f(Singer s){
s.sing();
}
//只要实现了这个接口的任何一个类的对象都可以往该方法里传 6.总结
新闻热点
疑难解答