62,615
社区成员
发帖
与我相关
我的任务
分享
package 测试类;
public class Test1 {
public static void main(String[] args){
Vip v = new Vip("张三");
v.shopping();
v.doSome();
System.out.println(v);
}
}
class Customer {
String name;
public Customer(){}
public Customer(String name){
super();
this.name = name;
}
public void doSome(){
System.out.println(this.name + " do some!父类");
System.out.println(name + " do some!父类");
System.out.println("Customer的toString: "+toString());
}
}
class Vip extends Customer{
String name; // 实例变量
public Vip(){
}
public Vip(String name){
super(name);
// this.name = null;
}
public void shopping(){
System.out.println(this.name + "正在购物!子类");
System.out.println(super.name + "正在购物!子类");
System.out.println(name + "正在购物!子类");
System.out.println("Vip的toString: "+toString());
}
}
/* 运行结果
null正在购物!子类
张三正在购物!子类
null正在购物!子类
Vip的toString: 测试类.Vip@1b6d3586
张三 do some!父类
张三 do some!父类
Customer的toString: 测试类.Vip@1b6d3586
测试类.Vip@1b6d3586
*/
我以前认为this在哪,指向的就是哪里,可现在看来我是错的,那我这个代码又该如何解释呢?[/quote]我也疑惑 继承后 子类调用继承的方法dosome为啥还是父类的属性[/quote]应该是当子类和父类的属性名相同时,遵循一个"就近原则",调用继承父类dosome方法就直接在父类的位置找这个属性值package 测试类;
public class Test1 {
public static void main(String[] args){
Vip v = new Vip("张三");
v.shopping();
v.doSome();
System.out.println(v);
}
}
class Customer {
String name;
public Customer(){}
public Customer(String name){
super();
this.name = name;
}
public void doSome(){
System.out.println(this.name + " do some!父类");
System.out.println(name + " do some!父类");
System.out.println("Customer的toString: "+toString());
}
}
class Vip extends Customer{
String name; // 实例变量
public Vip(){
}
public Vip(String name){
super(name);
// this.name = null;
}
public void shopping(){
System.out.println(this.name + "正在购物!子类");
System.out.println(super.name + "正在购物!子类");
System.out.println(name + "正在购物!子类");
System.out.println("Vip的toString: "+toString());
}
}
/* 运行结果
null正在购物!子类
张三正在购物!子类
null正在购物!子类
Vip的toString: 测试类.Vip@1b6d3586
张三 do some!父类
张三 do some!父类
Customer的toString: 测试类.Vip@1b6d3586
测试类.Vip@1b6d3586
*/
我以前认为this在哪,指向的就是哪里,可现在看来我是错的,那我这个代码又该如何解释呢?[/quote]我也疑惑 继承后 子类调用继承的方法dosome为啥还是父类的属性package 测试类;
public class Test1 {
public static void main(String[] args){
Vip v = new Vip("张三");
v.shopping();
v.doSome();
System.out.println(v);
}
}
class Customer {
String name;
public Customer(){}
public Customer(String name){
super();
this.name = name;
}
public void doSome(){
System.out.println(this.name + " do some!父类");
System.out.println(name + " do some!父类");
System.out.println("Customer的toString: "+toString());
}
}
class Vip extends Customer{
String name; // 实例变量
public Vip(){
}
public Vip(String name){
super(name);
// this.name = null;
}
public void shopping(){
System.out.println(this.name + "正在购物!子类");
System.out.println(super.name + "正在购物!子类");
System.out.println(name + "正在购物!子类");
System.out.println("Vip的toString: "+toString());
}
}
/* 运行结果
null正在购物!子类
张三正在购物!子类
null正在购物!子类
Vip的toString: 测试类.Vip@1b6d3586
张三 do some!父类
张三 do some!父类
Customer的toString: 测试类.Vip@1b6d3586
测试类.Vip@1b6d3586
*/
我以前认为this在哪,指向的就是哪里,可现在看来我是错的,那我这个代码又该如何解释呢?