class Human
{
String name;
String sex;
int age;
String adr;
void Huamn (String name, String sex, int age, String adr)
{
this.name = name;
this.sex = sex;
this.age = age;
this.adr = adr;
}
void work ()
{
System.out.println ("I am working" + this.name);
}
void eat ()
{
System.out.println ("I am eating");
}
}
/ / test main program :
public class Demo
{
public static void main (String [] args)
{
Human zhangsan = new Human (" Joe Smith ", " M ", 23 , " Beijing " ) ;
Human lisi;
lisi = new Human (" John Doe ", " M ", 12 , "Nanjing" ) ;
zhangsan.name = " Joe Smith " ;
System.out.println (zhangsan.name);
}
}
compiler can not pass , as follows:
A: \ jav> javac Demo.java
Demo.java: 6: Error : Unable to class in the constructor Human Human applied to a given type ;
Human zhangsan = new Human (" Zhang three "," M ", 23 , " Beijing " ) ;
^
need: no parameters
find : String, String, int, String
reasons : different actual parameter list and the formal parameter list length
Demo.java: 8: Error : Unable to class in the constructor Human Human applied to a given type ;
lisi = new Human (" John Doe " , "male" , 12, "Nanjing" ) ;
^
need: no parameters
find : String, String, int, String
reasons : different actual parameter list and the formal parameter list length
2 errors
------ Solution ------------------------------------ --------
void Huamn (String name, String sex, int age, String ; adr) {
wrong name , Human, then the constructor can not have a return type
into public Human [/ color] (String name, String sex, int age, String adr) {
------ Solution - -------------------------------------------
public Human(String name, String sex, int age, String adr) {
this.name = name;
this.sex = sex;
this.age = age;
this.adr = adr;
}
constructor wrong , no return value , another Human spelled wrong.
------ Solution ---------------------------------------- ----
beginner java to carefully
------ Solution ----------------------------- ---------------
... next eclipse it, this error ...
------ Solution --------- -----------------------------------
illustrated positive solution , the constructor does not return a value
- ----- For reference only ---------------------------------------
Yes, yes, thank you very much !
没有评论:
发表评论