Person have name field of Name type means i understood to create new Name class with fields firstname, middlename, and lastname but i dont know how to use this class in person to give him name?
can you please write code two two different class and show me how to use?
hi friend,
Try the following code may this will be helpful for you
package net.roseindia.simpleExample; public class Name { String firstName; String middleName; String lastName; public Name() { } public Name(String firstName, String middleName, String lastName) { this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String displayName() { return getFirstName()+" "+ getMiddleName()+" "+getLastName(); } }
Continue....
package net.roseindia.simpleExample; public class Person { Name name; public Person() { } public Person(Name name) { super(); this.name = name; } public void display() { System.out.println(name.displayName()); } }
Continue...
package net.roseindia.simpleExample; public class Main { public static void main(String args[]) { Name name = new Name(); name.setFirstName("Bipul"); name.setMiddleName("Kumar"); name.setLastName("Choudhary"); //Name name = new Name("Bipul", "Kumar", "Choudhary"); Person person = new Person(name); person.display(); } }
Ads