[英]Instantiating from a class that has an array parameter in constructor in java
I have looked at several posts to reach an answer, but none came close enough. 我看过几篇文章以找到答案,但没有一个足够接近。
I have a Q that might sound very empirical, and I am sure the answer is staring me in the face, but I am blinkered, I suppose! 我有一个Q听起来可能是非常有经验的,我相信答案正盯着我,但我想我眨了眨眼! I have a class named DrugExcretion with a constructor that has a parameter that is a reference to an array (named drugExcretionCode).
我有一个名为DrugExcretion的类,它带有一个构造函数,该构造函数的参数是对数组的引用(名为drugExcretionCode)。
I can't seem to work out the SYNTAX to instantiate (ie create an object) in my main class named DrugExcretionApp. 我似乎无法找出语法来实例化(即创建一个对象)在我的名为DrugExcretionApp主类。 Both classes are below:-
这两个类都在下面:
If anyone can direct me to a simple way to do this, it would be much appreciated. 如果有人可以指导我采用一种简单的方法来执行此操作,将不胜感激。 Thanks in advance.
提前致谢。
public class DrugExcretion implements CautionInterface{
int[] drugExcretionCode;
private String[] drug;
public DrugExcretion(String[] drug){
this.drug = drug;
}
public String determineDanger(int[] drugExcretionCode){
String site1 = "kidney";
String site2 = "liver";
String site;
if (drugExcretionCode = 1){
return "reduce dosage in elderly";
}
else{
return "reduce dosage in children";
}
}
}
x ----------------- o ----------------- x ----------------- o x ----------------- o ----------------- x ------------- ---- o
public class drugExcretionApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
final String drug1 = "enalapril";
final String drug2 = "captopril";
final String drug3 = "metoprolol";
final String drug4 = "amlodipine";
final String drug5 = "candesartan";
String drug[] = {drug1, drug2, drug3, drug4, drug5};
DrugExcretion listOne = new DrugExcretion(drug);
DrugExcretion.determineDanger(new int[]{1, 1, 2, 2, 1});
}
}
I get an error "DrugExcretion cannot be resolved to a type" which initially made me think that the project setup was incorrect so the DrugExcretion class could not be seen by the app class - I rearranged the classes to keep them in the same package, but the error persists. 我收到一个错误“ DrugExcretion无法解析为类型”,最初使我认为项目设置不正确,因此应用程序类无法看到DrugExcretion类-我重新排列了这些类以将它们保留在同一程序包中,但是错误仍然存在。
It appears to be a problem with access modifiers - have I made something static inadvertently??? 访问修饰符似乎存在问题-我是否无意间将静态内容制成了???
BTW, the interface simply contains a signature of the only method in the first class. 顺便说一句,该接口仅包含第一类中唯一方法的签名。
You have to call determineDanger
like this: 您必须像这样调用
determineDanger
:
listOne.determineDanger(new int[]{1, 1, 2, 2, 1]);
DrugExcretion.xxx
would be for a static method xxx
of DrugExcretion
. DrugExcretion.xxx
将是一个静态方法xxx
的DrugExcretion
。 But determineDanger
is an instance method, so needs to be invoked on an instance of the class, in this case listOne
. 但是
determineDanger
是一个实例方法,所以需要在类的实例来调用,在这种情况下, listOne
。
Since you are instantiating the DrugExcretion
class, you need to call 由于您要实例化
DrugExcretion
类,因此需要调用
listOne.determineDanger(new int[] { 1, 1, 2, 2, 1 });
Also, please check your code in the DrugExcretion
class. 另外,请检查
DrugExcretion
类中的代码。 It should be equals operator and not assignment. 它应该等于运算符而不是赋值。
if (drugExcretionCode == 1){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.