简体   繁体   English

我是否使用了正确的 getter/setter 方法?

[英]Am i using the proper getter/setter methods?

I'm currently learning how to code and on my first module final.我目前正在学习如何编码以及我的第一个模块决赛。 Before I can move on I must complete the assignment.在我可以继续之前,我必须完成任务。 When I input my code it returns with 1 error.当我输入我的代码时,它返回 1 个错误。 The code is listed below.代码如下所示。 Can someone tell me what I'm doing wrong and how to fix it?有人可以告诉我我做错了什么以及如何解决吗? Thank you!谢谢!

public class Driver extends User { 
    
    private Driveable vehicle = new Vehicle();
    
    public Driver() {}//mine
    public Driver(Vehicle vehicle, String name, String email){
        this();
        setVehicle(vehicle);
        setName(name);
        setEmail(email);
       }
 
 
  
     public Driveable getVehicle(){
      return vehicle;
     }
     public Drivable setVehicle(){
        this.vehicle;
     }
 
    
     public void setVehicle(Vehicle vehicle){
         this.vehicle = vehicle;
     }
    
     public void drive(){
        vehicle.drive();
     }
}

Error methods are listed below as well,下面也列出了错误方法,

            ^                                                                                                                                                                                                                                         
1 error                                                                                                                                                                                                                                               
class Driver does contain the required methods.                                                                                                                                                                                                       
Check class Vehicle  getter/setter methods.                                                                                                                                                                                                           
Exception in thread "main" java.lang.NoClassDefFoundError: TaxiService                                                                                                                                                                                
        at Test.taxiService(Test.java:56)                                                                                                                                                                                                             
        at Test.main(Test.java:16)                                                                                                                                                                                                                    
Caused by: java.lang.ClassNotFoundException: TaxiService                                                                                                                                                                                              
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)                                                                                                                                                                                 
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)                                                                                                                                                                                      
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)                                                                                                                                                                              
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)                                                                                                                                                                                      
        ... 2 more                                                                      

                                                                                                                                                          

I'm also new, so pls forgive any error I make.我也是新人,所以请原谅我犯的任何错误。

emm.. first things first on the third line of code private Driveable vehicle = new Vehicle(); emm .. 首先在第三行代码private Driveable vehicle = new Vehicle();

you should not be giving assignments here, and the Class types also doesn't match private Driveable vehicle;您不应该在这里分配作业,并且 Class 类型也与private Driveable vehicle; is enough.足够的。

if you still want to initialize the attribute, do it in the constructor method.如果您仍想初始化属性,请在构造函数方法中进行。

your getter looks fine, but in the first setter, I'm not sure what you want to do by taking no input but to return an object reference.您的 getter 看起来不错,但在第一个 setter 中,我不确定您想通过不输入来做什么,而是返回 object 参考。

Note that the vehicle is of type Driveable and it seems you want to use it as Vehicle type in your code after definition, so consider fixing that.请注意,车辆是 Driveable 类型,并且您似乎希望在定义后将其用作代码中的车辆类型,因此请考虑修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM