//父类
//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()+"条腿");
}
}