zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然 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); 
      }
    }
  • 相关阅读:
    解题:POI 2009 TAB
    解题:POI 2015 Pieczęć
    解题:POI 2013 Taxis
    解题:POI 2015 Kinoman
    题目1012:畅通工程(并查集)
    并查集深入分析
    题目1186:打印日期(日期计算)
    C/C++如何整行读入字符串?
    四种方法解决最大连续子序列和问题
    题目1011:最大连续子序列
  • 原文地址:https://www.cnblogs.com/tszr/p/10966582.html
Copyright © 2011-2022 走看看