weixin_55397741 2021-02-19 01:03 采纳率: 0%
浏览 71

JAVA小白求助求助

class Car{
    private String name;
    private double price;
    private Person person;
    public Car(String name, double price){
        this.name=name;
        this.price=price;
    }    
        public void setPerson(Person person){
            this.person=person;
        }
        public Person getPerson(){
            return this.person;
        } 
      public String getinfo(){
            return "汽车品牌型号:" + this.name+ "/t汽车的价值:" + this.price ;    
    }
}
class Person{
    private String name;
    private int age;
    private Car car;
    public Person(String name,int age){
        this.name=name;
        this.age=age;
        public void setCar(Car car){
            this.car = car;
        }
        public Car getCar(){
        return this.car;
        }
    }
    public String getinfo(){
            return "车主的姓名:" + this.name+ "/t车主的年龄:" + this.age ;    
    }
}
public class demo{
    public static void main(String args[]){
         Person person= new Person("批破爷",26);
         Car car= new Car("奥迪RS7","1580000.00");
         person.setCar(car);
         car.setPerson(person);
             System.out.println(person.getCar().getinfo());
    }
}

demo.java:26: 错误: 非法的表达式开始
                public void setCar(Car car){
                ^
demo.java:33: 错误: 需要class, interface或enum
        public String getinfo(){
               ^
demo.java:35: 错误: 需要class, interface或enum
        }
        ^

 

 

要怎么改

  • 写回答

2条回答 默认 最新

  • allway2 2021-02-19 07:34
    关注
    class Car {
    	private String name;
    	private double price;
    	private Person person;
    
    	public Car(String name, double price) {
    		this.name = name;
    		this.price = price;
    	}
    
    	public void setPerson(Person person) {
    		this.person = person;
    	}
    
    	public Person getPerson() {
    		return this.person;
    	}
    
    	public String getinfo() {
    		return "汽车品牌型号:" + this.name + "/t汽车的价值:" + this.price;
    	}
    }
    
    class Person {
    	private String name;
    	private int age;
    	private Car car;
    
    	public Person(String name, int age) {
    		this.name = name;
    		this.age = age;
    
    	}
    
    	public void setCar(Car car) {
    		this.car = car;
    	}
    
    	public Car getCar() {
    		return this.car;
    	}
    
    	public String getinfo() {
    		return "车主的姓名:" + this.name + "/t车主的年龄:" + this.age;
    	}
    }
    
    public class demo {
    	public static void main(String args[]) {
    		Person person = new Person("批破爷", 26);
    		Car car = new Car("奥迪RS7", 1580000.00);
    		person.setCar(car);
    		car.setPerson(person);
    		System.out.println(person.getCar().getinfo());
    	}
    }
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误