谁会Java啊 帮帮忙 急急急!!
发布网友
发布时间:2023-07-12 01:28
我来回答
共3个回答
热心网友
时间:2024-12-03 12:05
Animal
public class Animal {
private String name;
private int age;
public void shout() {
System.out.println("name=" + name + ",age=" + age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Dog
public class Dog extends Animal{
private String No;
public void speak(){
System.out.println("No="+No);
}
public String getNo() {
return No;
}
public void setNo(String no) {
No = no;
}
}
T3
public class T3 {
/**
* 按以下要求设计一个Animal类:
* 1、Animal包含name和age两个变量,输出name和age的shout()方法;
* 2、建立Dog类继承Animal,增加一个No变量,输出No的speak()方法;
* 3、建立T3测试类,创建Dog类的对象t3,给Dog类的所有对象赋值,调用所有方法输出值。
*/
public static void main(String[] args) {
Dog t3 = new Dog();
t3.setName("小狗");
t3.setAge(10);
t3.setNo("No1");
t3.shout();
t3.speak();
}
}
热心网友
时间:2024-12-03 12:05
// Animal类
class Animal {
// 变量
public String name;
public int age;
// 构造函数
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
// 输出name和age的shout()方法
public void shout() {
System.out.println("name: " + name + ", age: " + age);
}
}
// Dog类继承Animal
class Dog extends Animal {
// 增加一个No变量
public int no;
// 构造函数
public Dog(String name, int age, int no) {
super(name, age);
this.no = no;
}
// 输出No的speak()方法
public void speak() {
System.out.println("no: " + no);
}
}
热心网友
时间:2024-12-03 12:05
public class Animal {
public String name;
public int age;
public void shout() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Dog extends Animal {
public int No;
public void speak() {
System.out.println("No: " + No);
}
}
public class T3 {
public static void main(String[] args) {
Dog t3 = new Dog();
t3.name = "Bob";
t3.age = 3;
t3.No = 1001;
t3.shout();
t3.speak();
}
}