用java编程 定义一个Animal类,再定义它的两个子类Cat和Dog,并在Test类中创建Cat和Dog的对象.急 急 急 !
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/24 00:47:16
用java编程 定义一个Animal类,再定义它的两个子类Cat和Dog,并在Test类中创建Cat和Dog的对象.急 急 急 !
用java编程 定义一个Animal类,再定义它的两个子类Cat和Dog,并在Test类中创建Cat和Dog的对象.急 急 急 !
用java编程 定义一个Animal类,再定义它的两个子类Cat和Dog,并在Test类中创建Cat和Dog的对象.急 急 急 !
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Test {
\x05public static void main(String[] args) {
\x05\x05Cat cat = new Cat();
\x05\x05Dog dog = new Dog();
\x05\x05
\x05\x05cat.setColor("Yellow");
\x05\x05cat.setName("Kitty");
\x05\x05
\x05\x05dog.setCry("Wang wang wang");
\x05\x05dog.setName("Wang Cai");
\x05\x05
\x05\x05System.out.println("Cat's name:" + cat.getName());
\x05\x05System.out.println("Cat's color:" + cat.getColor());
\x05\x05
\x05\x05System.out.println("Dog's name:" + dog.getName());
\x05\x05System.out.println("Dog's cry:" + dog.getCry());
\x05}
}
class Animal{
\x05protected String name;
\x05public String getName() {
\x05\x05return name;
\x05}
\x05public void setName(String name) {
\x05\x05this.name = name;
\x05}
\x05
\x05
\x05
}
class Cat extends Animal{
\x05private String color;
\x05public String getColor() {
\x05\x05return color;
\x05}
\x05public void setColor(String color) {
\x05\x05this.color = color;
\x05}
\x05
\x05
}
class Dog extends Animal{
\x05private String cry;
\x05
\x05public String getCry(){
\x05\x05return cry;
\x05}
\x05public void setCry(String cry) {
\x05\x05this.cry = cry;
\x05}
\x05
\x05
}
-----------testing
Cat's name:Kitty
Cat's color:Yellow
Dog's name:Wang Cai
Dog's cry:Wang wang wang