java猫
发布时间: 2025-01-09 19:35:13
❶ java创建一个猫类Cat。具体要求如下 急急急在线等
publicclassCat{
privatestaticStringhobby="睡觉";
privateStringname;
privateintweight;
privateintage;
publicCat(){
this("",0,0);
}
publicCat(Stringname,intweight,intage){
this.name=name;
this.weight=weight;
this.age=age;
}
publicvoidclimbTree(Stringname,intheight){
System.out.println(name+"会爬树,能爬"+height);
}
//省略getter//setter方法.
@Override
publicStringtoString(){
return"名字:"+this.name+" "+"体重:"+this.weight+" "+"年龄:"+this.age;
}
}
publicclassMain{
publicstaticvoidmain(String[]args){
CatcatA=newCat();
CatcatB=newCat("兰兰",8,2);
catA.setName("花花");
catA.setWeight(10);
catA.setAge(3);
//打印猫的信息
System.out.println(catA);
System.out.println(catB);
catA.climbTree(catA.getName(),-9999);
catB.climbTree(catB.getName(),99999);
}
}
未测试
❷ java程序 一个主人有两只宠物,一条狗一只猫,狗叫旺财,猫叫小花,分别给宠物喂食
publicclassPerson{
publicstaticvoidmain(Stringargs[]){
Animala1=newAnimal("猫","小花","鱼");
Animala2=newAnimal("狗","旺财","骨头");
a1.eat();a2.ert();
}
}
classAnimal{
Stringspecies;
StringanimalName;
StringfoodName;
Animal(Stringspecies,StringanimalName,StringfoodName){
this.species=species;
this.animalName=animalName;
this.foodName=foodName;
}
publicvoideat(){
System.out.print("我的"+species+","+animalName+"吃了:"+foodName);
}
}
//我也才学java不到一个月,我就能理解这么多
//还可以写继承的。亦可以写成多线程的。
//继承就是把Animal类当父类,再写两个类继承它。这里又可以复习多态,哈哈。
//多线程就是同事喂这俩动物。
❸ Java 猫吃鱼狗吃肉
public class Cat{
public void eat(String fish){
system.out.println("吃" + fish)
}
}
public class Dog{
public void eat(String meat){
system.out.println("吃" + meat)
}
}
main(){
new Cat().eat("鱼");
new Dog().eat("肉");
}
❹ java建立猫cat类和狗类,具备叫声的方法cry(),
/*定义动物父类*/
classAnimal{
voidcry(){
System.out.println("Animalcry");
}
}
classCatextendsAnimal{
voidcry(){
System.out.println("catcry");
}
}
classDogextendsAnimal{
voidcry(){
System.out.println("dogcry");
}
}
publicclassExe2{
publicstaticvoidcry(Animalanimal){
animal.cry();
}
publicstaticvoidmain(String[]args){
Animalanimal=newAnimal();
cry(animal);
Catcat=newCat();
cry(cat);
Dogdog=newDog();
cry(dog);
}
}
热点内容