Home > database >  How to use it in other classes when you change the properties of an instance in Java
How to use it in other classes when you change the properties of an instance in Java

Time:09-24

I'm started studying Java this time.
I'm having fun learning it, but there are parts I don't understand, so I ask.

Below is the code I wrote.

public class Info {
    String name;
    String company;
    
    Info(String n, String c) {
        name = n;
        company = c; 
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    
    public String toString() {
        return this.name   ". working at "   this.company;
    }
   
}

public class Info_C {
    String position;
    String dept;
    String name;
    String company; 
    
    Info_C(Info i, String p, String d) {
        name = i.name;
        company = i.company; 
        position = p;
        dept = d;
    }
    public void setInfo(Info i) {
        this.name = i.name;
        this.company = i.company;
    }
    public void setPosition(String p) {
        this.position = p;
    }
    
    public String toString() {
        return this.name   ". Working as a "  this.position   " in "   this.company   "'s "   this.dept;
    }

    public static void main(String[] args) {
        var Pf_B = new Info("Bob","Apple");
        var Pf_J = new Info("James","Google");
        
        var TM = new Info_C(Pf_B, "Team Manager","Sales Team1");
        var AM = new Info_C(Pf_B, "Assistane Manager","HR Team");
        var RE = new Info_C(Pf_B, "Research Engineer","R&D Team");
        
        System.out.println(Pf_B); // Bob. working at Apple
        System.out.println(Pf_J); // James. working at Google
        System.out.println(TM);   // Bob. Working as a Team Manager in Apple's Sales Team1
        System.out.println(AM);   // Bob. Working as a Assistane Manager in Apple's HR Team
        System.out.println(RE);   // Bob. Working as a Research Engineer in Apple's R&D Team
        
        Pf_B.setName("Eric");
        Pf_B.setCompany("Samsung");
        AM.setPosition("Team Manager");
        RE.setInfo(Pf_J);
        
        System.out.println("=========================Changed==========================");
        
        System.out.println(Pf_B); // Eric. working at Samsung
        System.out.println(TM);   // Bob. Working as a Team Manager in Apple's Sales Team1 
                                  // Why Bob. Apple? Thought Eric. Samsung
        System.out.println(AM);   // Bob. Working as a Team Manager in Apple's HR Team
                                  // Same here
        System.out.println(RE);   // James. Working as a Research Engineer in Google's R&D Team
    }
}
 

When modifying the instance attribute of the Info class, is there any way to make it applied to the instance attribute of Info_C?

Tried a lot and searched hard, but failed. perhaps, it's because of my poor English.
I can't move on to the next chapter because of my curious. Any help would be appreciated.

) I'm not even sure if the title is a good title for me, but I'd appreciate it if you let me know if it needs to be corrected.

CodePudding user response:

Your problem is that you are changing the name and company of the Info object, but that does not change said values in the Info_C object. To solve this problem, I would change your code to this -

public class Info {
    String name;
    String company;
    
    Info(String n, String c) {
        name = n;
        company = c; 
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    
    public String toString() {
        return this.name   ". working at "   this.company;
    }
   
}

public class Info_C {
    String position;
    String dept;
    Info info; 
    
    Info_C(Info i, String p, String d) {
        position = p;
        dept = d;
        info = i;
    }
    public void setInfo(Info i) {
        info = i;
    }
    public void setPosition(String p) {
        this.position = p;
    }
    
    public String toString() {
        return info.name   ". Working as a "  this.position   " in "   info.company   "'s "   this.dept;
    }

    public static void main(String[] args) {
        var Pf_B = new Info("Bob","Apple");
        var Pf_J = new Info("James","Google");
        
        var TM = new Info_C(Pf_B, "Team Manager","Sales Team1");
        var AM = new Info_C(Pf_B, "Assistane Manager","HR Team");
        var RE = new Info_C(Pf_B, "Research Engineer","R&D Team");
        
        System.out.println(Pf_B); // Bob. working at Apple
        System.out.println(Pf_J); // James. working at Google
        System.out.println(TM);   // Bob. Working as a Team Manager in Apple's Sales Team1
        System.out.println(AM);   // Bob. Working as a Assistane Manager in Apple's HR Team
        System.out.println(RE);   // Bob. Working as a Research Engineer in Apple's R&D Team
        
        Pf_B.setName("Eric");
        Pf_B.setCompany("Samsung");
        AM.setPosition("Team Manager");
        RE.setInfo(Pf_J);
        
        System.out.println("=========================Changed==========================");
        
        System.out.println(Pf_B); // Eric. working at Samsung
        System.out.println(TM);   // Bob. Working as a Team Manager in Apple's Sales Team1 
                                  // Why Bob. Apple? Thought Eric. Samsung
        System.out.println(AM);   // Bob. Working as a Team Manager in Apple's HR Team
                                  // Same here
        System.out.println(RE);   // James. Working as a Research Engineer in Google's R&D Team
    }
}

Please note that not setting your class' member variables to private is not ideal, and the access to them should be done using getters and setters only.

CodePudding user response:

You are looking for something like this:

public class Info_C extends Info

the base class, Info in this case will give its properties to the child class. you can call the constructor of the parent class with super().

Also see: https://www.w3schools.com/java/java_inheritance.asp

  •  Tags:  
  • java
  • Related