标签:bsp 生活 子类 技术分享 code end 面向 extend rod
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 void sum() { super.eat(); super.sleep(); super.introduction(); } }
老鼠类: public class Mouse extends Animal { public Mouse(String myName, int myid) { super(myName, myid); } public void sum() { super.eat(); super.sleep(); super.introduction(); } }
测试类: public class Test { public static void main(String[] args){ Mouse mouse = new Mouse("mouse", 1); mouse.sum(); Penguin penguin = new Penguin("penguin", 1); penguin.sum(); } }
super(); //调用父类无参构造函数 super("参数") //调用父类有参构造函数
super.属性名 //引用子类已继承的属性 super.方法名(参数列表) //引用子类已继承的方法
标签:bsp 生活 子类 技术分享 code end 面向 extend rod
原文地址:http://www.cnblogs.com/ytsbk/p/7436292.html