我得到的输出是"the name and age of student is:null and 0"为什么?


public class student
{ 
    private String name,int age; 
    public student () 
    { 
        String name = "Dominic"; 
        int age = 10; 
    } 
    public String getName() 
    { 
        return name; 
    } 
    public int getAge() 
    { 
        return age; 
    } 
    public static void main(String[] args) 
    {
        student s= new student();
        System.out.println("The name and age of employer is:");
        System.out.print(s.getName()+"tandt"+s.getAge());
    }
}

删除构造函数中变量上的类型声明。您通过添加字符串和int

在构造函数中声明新的变量名称和年龄

更改为

age = 10;
name = "Dominick";

这样,您将值分配给类字段而不是本地构造函数。还要考虑使用它来指定字段。

this.age = 10;
this.name="Dominick";

最新更新