Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

About attributes, methods in JAVA

I'm doing a program, and that program has student data.

In my Student class it has the following content:

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

In my 'Info' class it has the following content:

public class Info{

    static string name;
    static String address;
    static int age;

And in my 'Main' class it has the following content:

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;

}

How do I change the 'capital' attribute of the 'Student' class so that this attribute is now of the 'Info' type. nd how do I assign this instance to a 'student'?

question from:https://stackoverflow.com/questions/65831289/about-attributes-methods-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is the SAMPLE of Info class

public class Info {
    String capital;
    String state;
}

Student class is

public class Student {
    Info addressInfo;
}

And another class to call student

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);  
    }
}

I hope this answer meet your question


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...