public class Homeworker07 {
public static void main(String[] args) {
new Demo().test();
new Demo("john").test();
/*
1 Test 创建Demo对象 先调用父类无参构造
2 Demo 调用父类构造完毕 调用类本身无参构造
3 Rose test()方法中super.name
4 Jack test()方法中this.name
5 john 有参构造继承父类有参构造 并将john赋值给了父类的name test()中super.name 即为john
6 Jack test()中this.name 无变化 仍然为Jack
*/
}
}
class Test{
String name="Rose";
Test(){
System.out.println("Test");
}
public Test(String name) {
this.name = name;
}
}
class Demo extends Test{
String name="Jack";
public Demo() {
super();
System.out.println("Demo");
}
public Demo(String s) {
super(s);
}
public void test(){
System.out.println(super.name);
System.out.println(this.name);
}
}
obj作业4-super this区分
最新推荐文章于 2024-07-08 17:49:05 发布