吴裕雄--天生自然 JAVA开发学习:继承

Posted tszr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了吴裕雄--天生自然 JAVA开发学习:继承相关的知识,希望对你有一定的参考价值。

class 父类 

 
class 子类 extends 父类 
public class Penguin  
    private String name; 
    private int id; 
    public Penguin(String myName, int  myid)  
        name = myName; 
        id = myid; 
     
    public void eat() 
        System.out.println(name+"正在吃"); 
    
    public void sleep()
        System.out.println(name+"正在睡");
    
    public void introduction()  
        System.out.println("大家好!我是"         + id + "号" + name + "."); 
     
public class Mouse  
    private String name; 
    private int id; 
    public Mouse(String myName, int  myid)  
        name = myName; 
        id = myid; 
     
    public void eat() 
        System.out.println(name+"正在吃"); 
    
    public void sleep()
        System.out.println(name+"正在睡");
    
    public void introduction()  
        System.out.println("大家好!我是"         + id + "号" + name + "."); 
     
public class Animal  
    private String name;  
    private int id; 
    public Animal(String myName, int myid)  
        name = myName; 
        id = myid;
     
    public void eat() 
        System.out.println(name+"正在吃"); 
    
    public void sleep()
        System.out.println(name+"正在睡");
    
    public void introduction()  
        System.out.println("大家好!我是"         + id + "号" + name + "."); 
     
public class Penguin extends Animal  
    public Penguin(String myName, int myid)  
        super(myName, myid); 
     
public class Mouse extends Animal  
    public Mouse(String myName, int myid)  
        super(myName, myid); 
     
public class Animal  
    private String name;   
    private int id; 
    public Animal(String myName, String myid)  
        //初始化属性值
     
    public void eat()   //吃东西方法的具体实现   
    public void sleep()  //睡觉方法的具体实现   
 
 
public class Penguin  extends  Animal 
public interface A 
    public void eat();
    public void sleep();

 
public interface B 
    public void show();

 
public class C implements A,B 
class Animal 
  void eat() 
    System.out.println("animal : eat");
  

 
class Dog extends Animal 
  void eat() 
    System.out.println("dog : eat");
  
  void eatTest() 
    this.eat();   // this 调用自己的方法
    super.eat();  // super 调用父类方法
  

 
public class Test 
  public static void main(String[] args) 
    Animal a = new Animal();
    a.eat();
    Dog d = new Dog();
    d.eatTest();
  
class SuperClass 
  private int n;
  SuperClass()
    System.out.println("SuperClass()");
  
  SuperClass(int n) 
    System.out.println("SuperClass(int n)");
    this.n = n;
  

// SubClass 类继承
class SubClass extends SuperClass
  private int n;
  
  SubClass() // 自动调用父类的无参数构造器
    System.out.println("SubClass");
    
  
  public SubClass(int n) 
    super(300);  // 调用父类中带有参数的构造器
    System.out.println("SubClass(int n):"+n);
    this.n = n;
  

// SubClas2 类继承
class SubClass2 extends SuperClass
  private int n;
  
  SubClass2()
    super(300);  // 调用父类中带有参数的构造器
    System.out.println("SubClass2");
    
  
  public SubClass2(int n) // 自动调用父类的无参数构造器
    System.out.println("SubClass2(int n):"+n);
    this.n = n;
  

public class TestSuperSub
  public static void main (String args[])
    System.out.println("------SubClass 类继承------");
    SubClass sc1 = new SubClass();
    SubClass sc2 = new SubClass(100); 
    System.out.println("------SubClass2 类继承------");
    SubClass2 sc3 = new SubClass2();
    SubClass2 sc4 = new SubClass2(200); 
  

 

以上是关于吴裕雄--天生自然 JAVA开发学习:继承的主要内容,如果未能解决你的问题,请参考以下文章

吴裕雄--天生自然 JAVA开发学习:变量类型

吴裕雄--天生自然 JAVA开发学习:多线程编程

吴裕雄--天生自然 JAVA开发学习:日期时间

吴裕雄--天生自然 JAVA开发学习:发送邮件

吴裕雄--天生自然 JAVA开发学习:网络编程

吴裕雄--天生自然 JAVA开发学习:多态