(笔记)关于java中的一些有关问题

(笔记)关于java中的一些问题第一题:?问题:1) Abstract methods do not specify a body??????????2) This

(笔记)关于java中的一些问题

第一题:


?问题:1) Abstract methods do not specify a body;

??????????2) This method must return a result of type boolean;

????????? 3) The field Name.name is never read locally;

?

第二题:


?

报错:

1)Implicit(不明确的) super constructor A() is undefined for default constructor. Must??define an explicit (明确的)constructor;

2)如果删除A类中的构造方法,这个程序可以通过编译但是由于S没有赋值输出的结果为null;

?

修改:

输出结果;

Parent_constructor
Child_test,name=null
Child_test,name=张三丰
Child_constructor

总结:

1:当父类不含有construtor;

???? a.子类没有override父类的方法

??????? 子类实例化对象可以直接调用父类的方法和属性;如果属性父类和子类有相同的属性,在调用父类的属性时,通过Parent p=new Child();可以获取parent的属性...

?

???? ?如果属性父类和子类有相同的属性,在调用父类的属性时,通过Parent p=new Child();可以获取parent的属性...

??

???? b.子类overrride父类的方法

??????? 无论是通过子类实例化 还是父类通过子类实例化,因为父类的方法被子类override所以输出的结果都是一样;

?

?

输出:

Child_test,name=张三丰
Child_test,name=张三丰
张三丰
张二疯

?

2:当父类含有不含参数的construtor;

???? 子类的默认的construtor;继承了父类的construtor;

?

??

输出结果:

Parent_constructor
Child_test,name=null

2:当父类含有含参数的construtor;

??? 在子类中必须声明一个含参数的construtor,并super()继承,如第四题。

第七题:

父类:

class B extends ExamSuperA{int m=1,n=1;long f(){long result = 0;super.m=10;super.n=20;result = super.f()+(m+n);return result;}long g(){long result = 0;result = super.f()+(m+n);System.out.println(super.m);return result/2;}}public class ExamSuper {public static void main(String[] args) {B b = new B();b.m =3;b.n = 7;long resultOne = b.g();long resultTwo = b.f();long resultThree = b.g();System.out.println("resultOne="+resultOne);System.out.println("resultTwo="+resultTwo);System.out.println("resultThree="+resultThree);}}

?

输出结果:

0

10

resultOne=5

resultTwo=40

resultThree=20

?

总结:

通过子类实例化 b??

执行long resultOne = b.g(); super.m=0 所以输出的第一个结果为“0”;

执行long resultTwo = b.f();由于子类override父类的,所以此时调用的f()方法是子类的,子类f()方法中又将super.m;和super.n重新赋值;

执行long resultOne = b.g();其中调用的Super.f()中m.n属性的值已经通过子类修改,所以super.f()的值也发生了改变...

特别注意:super.f()中的属性被修改了 区别于:为什么在Operate这个方法中运行后,X的值改变,但Y没有?

?

?

?

Child c=new Child();

Parent p=new Child();

对象c不仅有自己属性和方法,还继承了父类的方法和属性;

如果子类将父类的方法override了,那么用对象c调用的方法为Child 类的方法;p的方法也将被子类的方法override了,也就是说p的方法被override了,原来的方法不存在了。

如果子类将父类的属性同名,可以通过p来调用parent的同名属性。

?