java集成参考代码,萌翻你的心

//父类

//java中的继承中一共分为三步

1.属性私有化

2.构造方法

3.方法重载

public class Dog {

//boolean用get和is  其他类型用set和get

        //第一步:属性私有化

private boolean mammal;//是否是哺乳动物
private boolean Carnivorous;//是否是食肉动物
private int mood;//有几条腿
private int numOfLegs;//设置情绪值

//第二步:构造方法

       //  1.有参构造方法

public  Dog(boolean mammal, boolean carnivorous, int mood, int numOfLegs) {

super();
this.mammal = mammal;
Carnivorous =carnivorous;
this.mood = mood;
this.numOfLegs = numOfLegs;

}

    //   1.无惨构造方法,

public  Dog() {
this.mammal = true;
this.Carnivorous = true;
this.mood = 2;
this.numOfLegs = 4;
}

public boolean isMammal() {
return mammal;
}
public void setMammal(boolean mammal) {
this.mammal = mammal;
}
public boolean isCarnivorous() {
return Carnivorous;
}
public void setCarnivorous(boolean carnivorous) {
Carnivorous = carnivorous;
}
public int getMood() {
return mood;

}

       //第三步:方法重载

public void setMood(int mood) {
if(mood>0 && mood<4){
this.mood = mood;
}else{
System.out.println("输入有误,请重新输入");
}

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

}

//继承类,子类

 public class DogTest {


public static void main(String[] args) {

//创建对象,java中皆为对象

    Dog taozi= new Dog();
    if(taozi.isMammal()==true){
    System.out.println("狗是哺乳动物");
    }else{
    System.out.println("狗不是哺乳");
    }
     System.out.println("一般情况下,狗的打招呼方法是:"+taozi.sayHello());
     System.out.println("狗高兴的时候,打招呼是"+taozi.sayHello(1));
     System.out.println("狗有"+taozi.getNumOfLegs()+"条腿");
     System.out.println("第二只狗");
     Dog xingzi= new Dog(true,true,2,1);
     if(taozi.isMammal()==true){
      System.out.println("狗是哺乳动物");
     }else{
      System.out.println("狗不是哺乳");
     }
      System.out.println("一般情况下,狗的打招呼方法是:"+xingzi.sayHello());
      System.out.println("狗高兴的时候,打招呼是"+xingzi.sayHello(2));
      System.out.println("狗有"+xingzi.getNumOfLegs()+"条腿");
     }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值