Java面向对象中的继承

动物类的继承:利用继承写出动物的属性和sayhello的方法.

首先先创建一个父类
public class Animal {
protected boolean mammal;//是否肉食动物
protected boolean carnivorous;//是否哺乳动物
protected int mood ;//情绪
public boolean isMammal() {
return mammal;
}
public void setMammal(boolean mammal) {
this.mammal = mammal;
}
public boolean isCarnivorous() {
return carnivorous;
}
public void setCarnivorous(boolean carnivorous) {
this.carnivorous = carnivorous;
}
public int getMood() {
return mood;
}
public void setMood(int mood) {
this.mood = mood;
}
public String sayhello(){
return "hello!";
}
public String sayhello(int mood ){
switch(mood){
case 1:
return"不高兴的叫";
case 2:
return"高兴的叫";
default:
return "hello";
}
}
public Animal(){
System.out.println("这是Animal的无参构造方法!");
}
public Animal(boolean mammal, boolean carnivorous, int mood) {
System.out.println("这是Animal的有参构造方法!");
this.mammal = mammal;
this.carnivorous = carnivorous;
this.mood = mood;
}

}


    创建一个子类:继承父类
public class Dog extends Animal {
int  numOflegs;
public Dog(){
this.carnivorous = true;
this.numOflegs =4;
this.mammal = true;
this.mood = 1;
}
public Dog(boolean mammal,boolean carnivorous,int mood,int numOflegs ) {
super(mammal,carnivorous,mood);

this.numOflegs = numOflegs;
}
public String sayhello() {
return "汪汪叫";
}
public String sayhello(int mood) {
this.setMood(mood);
switch(mood){
case 1:
return"蹭蹭你的大腿";
case 2:
return"咬你一口";
default:
return"汪汪叫";
}
}

}

public class Frog extends Animal {
private int numOfLegs;
public Frog(){
this.carnivorous  = false;
this.numOfLegs =4;
this.mammal = true;
this.mood = 1;


}
public int getNumOfLegs() {
return numOfLegs;
}
public void setNumOfLegs(int numOfLegs) {
this.numOfLegs = numOfLegs;
}

然后创建测试类



        public class TestDog {


public static void main(String[] args) {
Dog dog1 = new Dog();
if(dog1.mammal==true){
System.out.println("狗狗是肉食动物");
}else{
System.out.println("狗狗不是肉食动物");
}
if(dog1.carnivorous == true){
System.out.println("狗狗是哺乳动物");
}else{
System.out.println("狗狗不是哺乳动物");
}
System.out.println("一般情况下:狗打招呼的方式是"+dog1.sayhello());
System.out.println("狗狗不高兴的时候"+dog1.sayhello(1));
System.out.println("狗狗不高兴了"+dog1.sayhello(2));
System.out.println("狗狗有"+dog1.numOflegs+"条腿");
System.out.println("-----------------------------");
Dog dog2 = new Dog(true,true,1,6);
if(dog2.mammal==true){
System.out.println("狗狗是肉食动物");
}else{
System.out.println("狗狗不是肉食动物");
}
if(dog2.carnivorous == true){
System.out.println("狗狗是哺乳动物");
}else{
System.out.println("狗狗不是哺乳动物");
}
System.out.println("一般情况下:狗打招呼的方式是"+dog2.sayhello());
System.out.println("狗狗不高兴的时候"+dog2.sayhello(1));
System.out.println("狗狗不高兴了"+dog2.sayhello(2));
System.out.println("狗狗有"+dog2.numOflegs+"条腿");

}

输出如下图:



public class TestFrog {


public static void main(String[] args) {

Frog frog1 = new Frog();
if(frog1.mammal==true){
System.out.println("青蛙是肉食动物");
}else{
System.out.println("青蛙不是肉食动物");
}
if(frog1.carnivorous == true){
System.out.println("青蛙是哺乳动物");
}else{
System.out.println("青蛙不是哺乳动物");
}
System.out.println("一般情况下:青蛙打招呼的方式是"+frog1.sayhello());
System.out.println("青蛙不高兴的时候"+frog1.sayhello(1));
System.out.println("青蛙不高兴了"+frog1.sayhello(2));
System.out.println("青蛙有"+frog1.getNumOfLegs()+"条腿");
System.out.println("-----------------------------");
Frog frog2 = new Frog(false,true,1,6);
if(frog2.mammal==true){
System.out.println("青蛙是肉食动物");
}else{
System.out.println("青蛙不是肉食动物");
}
if(frog2.carnivorous == true){
System.out.println("青蛙是哺乳动物");
}else{
System.out.println("青蛙不是哺乳动物");
}
System.out.println("一般情况下:青蛙打招呼的方式是"+frog2.sayhello());
System.out.println("青蛙不高兴的时候"+frog2.sayhello(1));
System.out.println("青蛙不高兴了"+frog2.sayhello(2));
System.out.println("青蛙有"+frog2.getNumOfLegs()+"条腿");

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值