关于JAVA中的属性和方法

关于JAVA中的属性和方法,java,methods,Java,Methods,我正在做一个程序,这个程序有学生数据 在我的学生课程中,它有以下内容: public class Student{ String capital; double note; int registration; } public class Info{ static string name; static String address; static int age; public class Main { public static

我正在做一个程序,这个程序有学生数据

在我的
学生
课程中,它有以下内容:

public class Student{
    String capital;
    double note;
    int registration;
}
public class Info{

    static string name;
    static String address;
    static int age;
public class Main {
    public static void main(String [] args){

        currentStudent Student = New Student ();

        current.Student.capital= "Peter";
        currentStudent.note = 8.0;
        currentStudent.registration= 876;


        Info f = new Info();

        Info.name= "Peter";
        Info.address= "Boulevard treet";
        Info.age= 43;

}
在我的“信息”类中,它包含以下内容:

public class Student{
    String capital;
    double note;
    int registration;
}
public class Info{

    static string name;
    static String address;
    static int age;
public class Main {
    public static void main(String [] args){

        currentStudent Student = New Student ();

        current.Student.capital= "Peter";
        currentStudent.note = 8.0;
        currentStudent.registration= 876;


        Info f = new Info();

        Info.name= "Peter";
        Info.address= "Boulevard treet";
        Info.age= 43;

}
在我的“主要”课程中,它有以下内容:

public class Student{
    String capital;
    double note;
    int registration;
}
public class Info{

    static string name;
    static String address;
    static int age;
public class Main {
    public static void main(String [] args){

        currentStudent Student = New Student ();

        current.Student.capital= "Peter";
        currentStudent.note = 8.0;
        currentStudent.registration= 876;


        Info f = new Info();

        Info.name= "Peter";
        Info.address= "Boulevard treet";
        Info.age= 43;

}

如何更改“Student”类的“capital”属性,使该属性现在为“Info”类型。如何将此实例分配给“学生”?

这是Info类的示例

public class Info {
    String capital;
    String state;
}
学生班是

public class Student {
    Info addressInfo;
}
还有一个班级叫学生

public class School {
    public static void main(String[] args) {
        Student s = new Student();
        Info i = new Info();
        i.capital = "Chandigarh";
        i.state = "Haryana";
        s.addressInfo = i;
        System.out.println(s.addressInfo.state);
        System.out.println(s.addressInfo.capital);  
    }
}

我希望这个答案能满足您的问题

为什么
Info
类中的所有数据都是静态的?另外,您的
Student
类没有“owner”属性,因此如果您想要一个,只需制作一个并正确键入即可。但是请仔细阅读如何编写类,以及如何编写设置实例值的构造函数,因为它们的思想是传递所有希望对象实例具有的值作为构造函数调用的一部分。这就是它的工作:“构造”您的实例。需要无参数构造然后直接赋值作为单独步骤的代码很少。您的
学生
类没有
所有者
字段。你只是想添加一个
Info
类型吗?对不起,我是java初学者,我不知道很多事情。我也意识到我写了一些错误的信息,我只是改正了。因此,基本上我需要将“Student”类的“capital”属性设置为“Info”类型。Java是一种强类型语言。如果将类字段定义为
字符串大写则大写字母只能是字符串。因此,如果您的问题是如何将
Info
类型的对象分配给
String capital
,那么简单的答案是:您不能。这是不可能的,但问题的类型是'Info'而不是'String'。也许是信息资本(?)