上传了实验四,已确认无敏感信息

This commit is contained in:
2024-06-10 16:42:32 +08:00
parent 2da90eeebf
commit c9d2aa581b
16 changed files with 324 additions and 0 deletions
+129
View File
@@ -0,0 +1,129 @@
package case1;
import java.util.Scanner;
public class Task1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String dogName = sc.next();
String dogSex = sc.next();
String dogColor = sc.next();
String catName = sc.next();
String catSex = sc.next();
double catWeight = sc.nextDouble();
// 通过有参构造函数实例化Dog类对象dog
// dog调用talk()方法
// dog调用eat()方法
/********* begin *********/
Dog lucky = new Dog(dogName, dogSex, dogColor);
lucky.talk();
lucky.eat();
/********* end *********/
// 通过有参构造函数实例化Cat类对象cat
// cat调用talk()方法
// cat调用eat()方法
/********* begin *********/
Cat 哈基米 = new Cat(catName, catSex, catWeight);
哈基米.talk();
哈基米.eat();
/********* end *********/
}
}
// 抽象类Pet 封装属性name和sex
// 构造函数初始化name和sex
// 声明抽象方法talk()
// 声明抽象方法eat()
abstract class Pet {
/********* begin *********/
protected String name;
protected String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public abstract void talk();
public abstract void eat();
/********* end *********/
}
// Dog类继承自Pet类 封装属性color
// 构造函数初始化name、sex和color
// 实现自己的talk()方法和eat()方法
// talk()输出'名称:name,性别:sex,颜色:color,汪汪叫'
// eat()输出'name吃骨头'
class Dog extends Pet {
/********* begin *********/
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Dog(String name, String sex, String color) {
this.name = name;
this.sex = sex;
this.color = color;
}
public void talk() {
System.out.println("名称:" + this.name + ",性别:" + this.sex + ",颜色:" + this.color + ",汪汪叫");
}
public void eat() {
System.out.println(this.name + "吃骨头");
}
/********* end *********/
}
// Cat类继承自Pet类 封装属性weight
// 构造函数初始化name、sex和weight
// 实现自己的talk()方法和eat()方法
// talk()输出'名称:name,性别:sex,体重:weight kg,喵喵叫'
// eat()输出'name吃鱼'
class Cat extends Pet {
/********* begin *********/
private double weight;
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Cat(String name, String sex, double weight) {
this.name = name;
this.sex = sex;
this.weight = weight;
}
public void talk() {
System.out.println("名称:" + this.name + ",性别:" + this.sex + ",体重:" + this.weight + " kg,喵喵叫");
}
public void eat() {
System.out.println(this.name + "吃鱼");
}
/********* end *********/
}
View File