继承
需求:有一只猫,一直狗 他们都有四条腿 都有 颜色 都能跑 用继承的方法实现
package com.test.day01; //动物类 class Animal{ //属性 private String colour; private int foot; private int age; public void run(){ System.out.println("跑的快"); } public Animal() { // super(); // TODO Auto-generated constructor stub } public Animal(String colour, int foot, int age) { //super(); this.colour = colour; this.foot = foot; this.age = age; } public String getColour() { return colour; } public void setColour(String colour) { this.colour = colour; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } class Dog extends Animal{ private String smell ; public void kanmen(){ System.out.println("狗会看门"); } public void run(){ System.out.println("边跑边叫"); } public Dog(String smell) { super(); this.smell = smell; } public String getSmell() { return smell; } public void setSmell(String smell) { this.smell = smell; } } class Cat extends Animal{ private String tail; public void run(){ System.out.println("连跑带跳"); } public void zhuaYu(){ System.out.println("抓鱼的方法"); } public Cat(String tail) { //super(); this.tail = tail; } public String getTail() { return tail; } public void setTail(String tail) { this.tail = tail; } } public class TestHeres { public static void main(String[] args) { // TODO Auto-generated method stub Dog dog = new Dog("灵敏"); //dog.setSmell("灵敏"); System.out.println(dog.getSmell()); Cat cat = new Cat("dawang"); cat.getTail(); cat.setColour("4"); System.out.println(cat.getColour()); } }
浙公网安备 33010602011771号