问题描述
- 实际参数列表与形式参数列表不同 ?为何会如此?
-
class Person
{
String name;
int age;
public void Person(String name,int age){
this.name = name;
this.age = age;
}
public void say(){System.out.println("姓名"+ this.name +" 年龄"+ this.age);
}
}public class AddPerson{
public static void main(String[] args){
Person p1 = new Person("王五",23);Person p2 = new Person("赵六",34);
Person p3 = new Person();
p3.name = "张三";
p3.age = 32;p1.say(); p2.say(); p3.say(); }
解决方案
public void Person(String name,int age){
this.name = name;
this.age = age;
}
这个地方错了,应该是这样的
public Person(String name,int age){
this.name = name;
this.age = age;
}
时间: 2024-10-06 23:15:26