前面的文章简单的分析了动态绑定机制,
http://blog.itpub.net/29876893/viewspace-1816523/,但是也许有很多疑问,下面介绍下子类如何调用自己的方法或我们阻止多态。
修改的代码如下:
上面在Cat,Dog中添加了新的方法,如果用Animal的引用调用这些方法,编译器会提错,那如果真想调用基类的自定义的方法,则需要强制转换,把Animal 引用强制转换成Cat,Dog类型,为了防止出现异常,我们用instanceof来检查,返回布尔类型,具体用法:
boolean result = object instanceof class
Result:布尔类型。
Object:必选项。任意对象表达式。
class:必选项。任意已定义的对象类。
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
我们通过instanceof的检查后,基类可以调用自定义的方法
如果把父类中的enjoy()定义为static,private,final,那么就不会出现多态性,方法被定义指定为private隐式的定义为final,类的设计者不希望该方法被继承修改。被指明为static时,那么这个方法就是类的通性,不会随对象的改变而发生变化,那么也失去了多态。关于这方面,读者可以参考<>第八章和<>第五章,有很详细的介绍。
下面给出一个小例子:
我们想出现的结果是os,但是结果是lios,因为父类中的f()方法在子类中不可见,不会出现多态机制,如果清楚多态机制的话,我想原因一目了然。
修改上述代码:
输出结果:
0 os
1 super.i=0
可能对于为什么打印 w . i为0,因为在在new子类对象时,堆内分配两个不同地址的i,一个是从父类继承的i,一个是基类中的i,此时父类的引用只指向自己的对象中的成员变量,所以结果如上。其他的都很简单。
修改的代码如下:
点击(此处)折叠或打开
- public class Animal {
-
- public String name;
-
- Animal(String name){
-
- this.name = name;
- }
-
- 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 void catcolor(){
-
- System.out.println(eyesColor);
- }
- }
-
-
- public class Dog extends Animal{
-
- private String furColor;
-
- Dog(String n,String c){
-
- super(n);
- furColor = c;
- }
-
- public void enjoy(){
-
- System.out.println("狗叫声。。。");
- }
-
- public void dogfurcolor(){
-
- System.out.println(furColor);
- }
- }
-
-
- public class Test {
-
- public void cataddr(Animal per){
-
- System.out.print(per.name + " ");
-
- if(per instanceof Cat){
-
- Cat cat = (Cat)per;
-
- cat.catcolor();;
-
- }else if(per instanceof Dog){
-
- Dog dog = (Dog)per;
-
- dog.dogfurcolor();
- }
- else{
-
- System.out.println("空值");
- }
- }
-
- public static void main(String[] args){
-
- Test w = new Test();
- Cat c = new Cat("catname", "bule");
- Dog d = new Dog("dogname", "black");
- w.cataddr(c);
- w.cataddr(d);
- }
- }
上面在Cat,Dog中添加了新的方法,如果用Animal的引用调用这些方法,编译器会提错,那如果真想调用基类的自定义的方法,则需要强制转换,把Animal 引用强制转换成Cat,Dog类型,为了防止出现异常,我们用instanceof来检查,返回布尔类型,具体用法:
boolean result = object instanceof class
Result:布尔类型。
Object:必选项。任意对象表达式。
class:必选项。任意已定义的对象类。
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
我们通过instanceof的检查后,基类可以调用自定义的方法
如果把父类中的enjoy()定义为static,private,final,那么就不会出现多态性,方法被定义指定为private隐式的定义为final,类的设计者不希望该方法被继承修改。被指明为static时,那么这个方法就是类的通性,不会随对象的改变而发生变化,那么也失去了多态。关于这方面,读者可以参考<>第八章和<>第五章,有很详细的介绍。
下面给出一个小例子:
点击(此处)折叠或打开
- class t {
-
- private void f(){
-
- System.out.println("diyjie");
- }
- public static void main(String[] args){
-
- t w = new t1();
- w.f();
- }
- }
-
- public class t1 extends t{
-
- public void f(){
-
- System.out.println("os");
- }
-
- }
我们想出现的结果是os,但是结果是lios,因为父类中的f()方法在子类中不可见,不会出现多态机制,如果清楚多态机制的话,我想原因一目了然。
修改上述代码:
点击(此处)折叠或打开
- class t {
-
- public int i = 0;
-
- public void f(){
-
- System.out.println("lios");
- }
- }
-
- public class t1 extends t{
-
- public int i =1;
-
- public void geti(){
-
- System.out.print(super.i);
- }
- public void f(){
-
- System.out.println("os");
- }
-
- public static void main(String[] args){
-
- t w = new t1();
- System.out.print(w.i + " ");
- w.f();
-
- t1 w1 = new t1();
- System.out.print(w1.i +" "+"super.i=");
- w1.geti();
-
- }
-
- }
0 os
1 super.i=0
可能对于为什么打印 w . i为0,因为在在new子类对象时,堆内分配两个不同地址的i,一个是从父类继承的i,一个是基类中的i,此时父类的引用只指向自己的对象中的成员变量,所以结果如上。其他的都很简单。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29876893/viewspace-1816807/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29876893/viewspace-1816807/