package LeiDeJiCheng; public abstract class Animal { private String name; private int age; //构造器 public Animal(){ }//无参 public Animal(String name,int age){ this.name = name; this.age = age; }//有参 public abstract void eat(); public String getName(){ return name; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } } class Rabbit extends Animal{ public Rabbit(){ } public Rabbit(String name,int age){ super(name, age); } @Override public void eat(){ System.out.println("兔子吃胡萝卜"); } } class Dog extends Animal{ public Dog(){} public Dog(String name,int age){ super(name, age); } @Override public void eat(){ System.out.println("狗吃shit"); } } class Friog extends Animal { public Friog(){} public Friog(String name,int age){ super(name,age); } @Override public void eat(){ System.out.println("青蛙吃虫子"); } }
时间: 2023-06-01 15:03:59 浏览: 184
//测试类
public class Test {
public static void main(String[] args) {
Animal animal1 = new Rabbit("小白兔", 2);
Animal animal2 = new Dog("旺财", 3);
Animal animal3 = new Friog("青蛙", 1);
animal1.eat();
animal2.eat();
animal3.eat();
}
}
//输出:
//兔子吃胡萝卜
//狗吃shit
//青蛙吃虫子
相关问题
续写这段代码package animal; public abstract class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void makeSound();
public class Cat extends Animal {
private int age;
public Cat(String name, int age) {
super(name);
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void makeSound() {
System.out.println("Meow");
}
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
super(name);
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
@Override
public void makeSound() {
System.out.println("Woof");
}
}
public class Elephant extends Animal {
private int weight;
public Elephant(String name, int weight) {
super(name);
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
***
帮我解释一下这段代码package bean; public class User { private String user; private String pwd; private String name; private String xxx; private int age; public String getUser() { return user; } public
这段代码定义了一个名为User的Java Bean类,它有五个私有成员变量:user、pwd、name、xxx和age。它们的类型分别为String和int。这个类还有一些公共方法,包括getUser()方法,它返回user成员变量的值。这个类的主要作用是封装用户的信息,提供公共的接口供其他类调用。在Java中,Bean类通常用来表示数据对象。
阅读全文