我只是一个初级菜鸟,谢谢大家指导
根据如下需求,完成代码(按照标准格式写),然后在测试类中测试。
需求一:
手机类Phone
属性:品牌brand,价格price
行为:打电话call,发短信sendMessage,玩游戏,playGame
需求二:
人类Person
属性:姓名name,年龄age,性别gender
行为:学习study,睡觉sleep
需求三:
动物类Animal
属性:姓名name,年龄age
行为:吃饭eat
需求四:
狗类Dog
属性:姓名name,年龄age
行为:吃饭eat,看家lookHome
需求五:
猫类Cat
属性:姓名name,年龄age
行为:吃饭eat,抓老鼠catchMouse
需求六:
学生类Student
属性:语文成绩chinese,数学成绩math,英语成绩english
行为:求总成绩的方法getSum()
大家看看有没有比较简便一点的做法呢?
class work {
public static void main(String[] args) {
Phone p1 = new Phone();
p1.brand = "黑马";
p1.price = 19990;
System.out.println(p1.brand+"\t\t"+p1.price);
p1.call();
p1.sendMessage();
p1.playGame();
System.out.println("===================================================");
Person p2 = new Person();
p2.name = "程序员";
p2.age = 43;
p2.gender = "男";
System.out.println(p2.name+"\t"+p2.age+"\t"+p2.gender);
p2.study();
p2.sleep();
System.out.println("=================================");
Animal a = new Animal();
a.name = "tiger";
a.age = 7;
System.out.println(a.name+"\t"+a.age);
System.out.println();
a.eat();
System.out.println("=================================");
Dog d = new Dog();
d.name = "乐乐";
d.age = 2;
System.out.println(d.name+"\t"+d.age);
System.out.println();
d.eat();
System.out.println();
d.lookHome();
System.out.println("==================================================");
Student s = new Student();
s.chinese = 89;
s.math = 90;
s.english = 97;
System.out.println("语文成绩 = "+s.chinese+"\n"+"数学成绩 = "+s.math+"\n"+"英语成绩 = "+s.english+"\n");
}
}
class Phone {
String brand;
int price;
public void call(){
System.out.println("打电话");
}
public void sendMessage(){
System.out.println("发信息");
}
public void playGame(){
System.out.println("玩游戏");
}
}
class Person {
String name;
int age;
String gender;
public void study(){
System.out.println("喜欢学习");
}
public void sleep(){
System.out.println("不爱睡觉");
}
}
class Animal {
String name;
int age;
public void eat(){
System.out.println("我不爱吃牛肉了");
}
}
class Dog {
String name;
int age;
public void eat(){
System.out.println("请给我老虎肉,我要去遛弯");
}
public void lookHome(){
System.out.println("主人不在,我要呆家里开paty");
}
}
class Student {
int chinese;
int math;
int english;
public static void getSum(int a,int b ,int c){
int sum = a+b+c;
}
}
|
|