类继承

class TParent{
    
}

class TChild extends TParent{
    
}

派生类的构造函数,派生类调用基类的构造函数Super()

public class TTest {
    public static void main(String[] args) {
        TChild child = new TChild("Child");
        System.out.println(child.getName());
    }

}

class TParent{
    private String Name;
    
    public TParent(String _name){
        Name = _name;
    }
    
    public String getName() {
        return Name;
    }
}

class TChild extends TParent{
    public TChild(String _name){
        super(_name);
    }
}

覆盖基类方法

public class TTest {
    public static void main(String[] args) {
        TChild child = new TChild("Child");
        System.out.println(child.getName());
    }

}

class TParent{
    private String Name;
    
    public TParent(String _name){
        Name = _name;
    }
    
    public String getName() {
        return Name;
    }
}

class TChild extends TParent{
    public TChild(String _name){
        super(_name);
    }
    
    public String getName() {
        return "重写了派生类的方法调用基类"+super.getName();
    }
}

@Override强制编译器检查方法的签名与基类是否相同

 

posted on 2012-10-06 15:42  kiny  阅读(201)  评论(0编辑  收藏  举报