黑马程序员技术交流社区
标题:
面向对象的使用(请问我错在哪??)
[打印本页]
作者:
x837248435
时间:
2016-7-19 22:13
标题:
面向对象的使用(请问我错在哪??)
class demo_mian {
public static void main(String[] args) {
Phone p1 = new Phone();
p1.setBrand("魅族");
p1.setPrice(2688);
System.out.println(p1.getBrand() + "..." + p1.getPrice());
p1.call();
p1.sendMessage();
p1.playGame();
Person p2 = new Person();
p2.setName("盖伦");
p2.setAge(17);
System.out.println(p2.getName() + "..." + p2.getAge());
p2.study();
p2.sleep();
Animal p3 = new Animal();
p3.setName("熊猫");
p3.setAge(12);
System.out.println(p3.getName() + "..." + p3.getAge());
p3.eat();
Dog p4 = new Dog();
p4.setName("二哈");
p4.setAge(8);
System.out.println(p4.getName() + "..." + p4.getAge());
p4.eat();
p4.lookHome();
Cat p5 = new Cat();
p5.setName("汤姆");
p5.setAge(4);
System.out.println(p5.getName() + "..." + p5.getAge());
p5.eat();
p5.catchMouse();
Student p6 = new Student();
p6.setChinese(128);
p6.setMath(136);
p6.setEnglish(132);
System.out.println(p6.getChinese() + "..." + p6.getMath() + "..." + p6.getEnglish());
System.out.println(p6.getSum());
}
}
class Phone {
private String brand;
private int price;
public void setBrand(String brand) {
this.brand = brand;
}
public String getBrand() {
return this.brand;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return this.price;
}
public void call() {
System.out.println("打电话");
}
public void sendMessage() {
System.out.println("发短信");
}
public void playGame() {
System.out.println("玩游戏");
}
}
class Person {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge () {
return age;
}
public void study() {
System.out.println("学生能学习");
}
public void sleep() {
System.out.println("学生需要睡觉");
}
}
class Animal {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void eat() {
System.out.println("熊猫要吃竹子");
}
}
class Dog {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void eat() {
System.out.println("狗要吃骨头");
}
public void lookHome() {
System.out.println("狗会看家");
}
}
class Cat {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void eat() {
System.out.println("汤姆要吃杰瑞");
}
public void catchMouse() {
System.out.println("汤姆要抓杰瑞");
}
}
class Student {
private int chinese;
private int math;
private int english;
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getChinese() {
return chinese;
}
public void setMath(int math) {
this.math = math;
}
public int getmath() {
return math;
}
public void setEnglish(int english) {
this.english = english;
}
public int getEnglish() {
return english;
}
public int getSum() {
return chinese + math + english;
}
}
作者:
wj3725
时间:
2016-7-20 12:58
我勒个去 这么多 建议eclipse 自动查错
作者:
Frankle
时间:
2016-7-22 22:58
Student类中的getmath()方法名写错了,应该是getMath()
作者:
李铮
时间:
2016-7-22 23:52
这么长 干看还真看不出开
作者:
yin12310
时间:
2016-7-23 00:11
真心不想看这代码,太长了,语法问题都要看半天,更别说别的问题,希望能把错误的地方给贴出来。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2