Java:基本类定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39870923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Basic class definition
提问by Java123
Define the missing method. licenseNum is created as: (100000 * customID) + licenseYear. Sample output: Dog license: 77702014
定义缺失的方法。licenseNum 被创建为:(100000 * customID) + licenseYear。样本输出:狗牌:77702014
This is what I did:
这就是我所做的:
public void createLicenseNum() {
licenseNum = (licenseNum * 100000) + licenseYear;
return;
}
But it's incorrect. Where am I going wrong?
但这是不正确的。我哪里错了?
CallDogLicense. java:32: createLicenseNum() in DogLicense cannot be applied to (int) dog1.createLicenseNum(777);
呼叫狗执照。java:32:DogLicense 中的 createLicenseNum() 不能应用于 (int) dog1.createLicenseNum(777);
Code from file DogLicense.java
来自文件 DogLicense.java 的代码
public class DogLicense {
private int licenseYear;
private int licenseNum;
public void setYear(int yearRegistered) {
licenseYear = yearRegistered;
return;
}
// FIXME: Write createLicenseNum()
/* Your solution goes here */
public void createLicenseNum() {
licenseNum = (licenseNum * 100000) + licenseYear;
return;
}
public int getLicenseNum() {
return licenseNum;
}
}
Code from file CallDogLicense.java
来自文件 CallDogLicense.java 的代码
public class CallDogLicense {
public static void main (String [] args) {
DogLicense dog1 = new DogLicense();
dog1.setYear(2014);
dog1.createLicenseNum(777);
System.out.println("Dog license: " + dog1.getLicenseNum());
return;
}
}
回答by Robby Cornelissen
You need to add the customID
argument to your method and use it in your calculation:
您需要将customID
参数添加到您的方法中并在您的计算中使用它:
public void createLicenseNum(int customID) {
licenseNum = (customID * 100000) + licenseYear;
}
回答by Shadov
Your method doesn't have any arguments, it's not javascript. You need to do this:
您的方法没有任何参数,它不是 javascript。你需要这样做:
public void createLicenseNum(int customId) {...
public void createLicenseNum(int customId) {...
Now if you call it like this dog.createLicenseNum(500);
then inside that method variable named customId
will have a value 500
.
现在,如果您像这样调用它,dog.createLicenseNum(500);
那么在名为的方法变量customId
中将有一个 value 500
。
回答by xenteros
Your're calling the method with int
, however it's declared to take no parameters.
您正在使用 调用该方法int
,但它被声明为不带参数。
public void createLicenseNum(int licenseNum) {
this.licenseNum = (licenseNum * 100000) + licenseYear;
return;
Will solve the problem.
会解决问题。
Still it's not how we do that in Java. For constructing objects you should use constructor.
这仍然不是我们在 Java 中这样做的方式。对于构造对象,您应该使用构造函数。
public class DogLicense {
private int licenseYear;
private int licenseNum;
public void setYear(int yearRegistered) {
licenseYear = yearRegistered;
return;
}
public DogLicense(int num, int year) {
licenseNum = (num* 100000) + year;
}
public int getLicenseNum() {
return licenseNum;
}
}
public class CallDogLicense {
public static void main (String [] args) {
DogLicense dog1 = new DogLicense(777, 2014);
System.out.println("Dog license: " + dog1.getLicenseNum());
return;
}
}