java吧 关注:1,234,938贴子:12,702,085
  • 2回复贴,共1

各位大神,这代码是哪里出错了?

只看楼主收藏回复

class Person{
private String name;
private int age;
private Book book; //得到本人的书
public Person(String name,int age){
this.setName(name);
this.setAge(age);
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public void SetBook(Book book){ //设置本人得到书
this.book = book;
}
public Book getBook(){
return book;
}
public String getInfo(){
return "姓名:" + this.getName() + "、年龄:" + this.getAge();
}
}
class Book{
private String title;
private double price;
private Person per;
public Book (String title,double price){
this.setTitle(title);
this.setPrice(price);
}
public void setTitle(String title){
this.title = title;
}
public void setPrice(double price){
this.price = price;
}
public String getTitle(){
return this.title;
}
public double getPrice(){
return this.price;
}
public void setPer(Person per){
this.per = per;
}
public Person getPer(){
return this.per;
}
public String getInfo(){
return "书名:" + this.getTitle() + "、价格:" + this.getPrice();
}
}
public class Studyjava1{
public static void main (String args[]){
Person per1 = new Person("张三",19);
Person per2 = new Person("李四",22);
Book bk1 = new Book("java se",87.0);
Book bk2 = new Book("java ee",83.0);
System.out.println(bk1.getInfo());
per1.setBook(bk1);
per2.setBook(bk2);
bk1.setper(per1);
bk2.setper(per2);
}
}


IP属地:广东1楼2017-11-24 10:48回复
    大兄弟,你这大小写都写错了,最后是SetBook和setPer


    IP属地:福建2楼2018-03-09 09:53
    回复
      帮你改了一下,
      要记住方法名一般用的是驼峰命名法
      https://baike.baidu.com/item/骆驼命名法?fromtitle=驼峰命名法&fromid=7560610
      方法首字母小写,后面的单词首字母大写


      IP属地:广东3楼2018-03-09 10:24
      回复