constructor not accepting my information

。_饼干妹妹 提交于 2019-12-31 05:30:10

问题


so the constuctor is saying ) expected, error not a statement and ; expected

    Person num1, num2, num3;
    num1=new Person(Allison, 6600 Crescent Ave, 32, 9024231421);
    num2=new Person(George, 5251 Lakewood St, 24, 9024489216);
    num3=new Person(Michael, 2429 Inglis St, 56, 9024212345);

object class

   public Person() {
   }
//constructor allows programmer to define variable values in demo class
   public Person(String nm, String adr, int ag, long phn) {
    name=nm;
    address=adr;
    age=ag;
    phoneNumber=phn;
 }

回答1:


num1=new Person(Allison, 6600 Crescent Ave, 32, 9024231421);

should be

num1=new Person("Allison", "6600 Crescent Ave", 32, 9024231421);

String, String, int and long are expected in this order by your constructor, which is defined by public Person(String nm, String adr, int ag, long phn).

Allison without (double)quotes is not a String.




回答2:


Step 1:

First you can check your Person class object is created.

Print some message in

 public Person() {
      System.out.println("in default constructor");
 }

if you can not see any print statement then problem with object.

Step 2:

Check what you pass and passed value is print or not.

If not print then problem with your parameter(argument).

Your Problem:

You are not passing actual string value,You have to pass string value wihin ""

num1=new Person("Allison", "6600 Crescent Ave", 32, 9024231421);

Many SO user give actually answer but this stuff is for feature reader so user can see and don't take mistake next time.




回答3:


You are not passing a string for Name and Address to your constructor, try changing

num1=new Person(Allison, 6600 Crescent Ave, 32, 9024231421);

to

num1=new Person("Allison", "6600 Crescent Ave", 32, 9024231421);



回答4:


you are calling construtor that in not exist for your first argument you have defined string so pass in double quote while calling construtor




回答5:


Because strings are always used within the double quotes . put your arguments which are strings in the double quotes.



来源:https://stackoverflow.com/questions/17357468/constructor-not-accepting-my-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!