当前位置: 动力学知识库 > 问答 > 编程问答 >

java - changing a persons name in the main from a different void method

问题描述:

So I'm trying figure out a way to change a persons last name from a different method in the main class. I'm not asking for the answer, just more guidance to help me learn. I'm still fairly new to programming and just trying to learn for my self

public class Family {

public static void main(String[] args) {

Person family = new Person();

printPerson(family);

family.setName("Juliet", "Montague");

printPerson(family);

}

public static void printPerson(Person member)

{

Person test = new Person();

member.setName("Juliet", "Capulet");

member.setDOB(7, 11, 1987);

member.setHeight(68);

System.out.println(member.getName());

System.out.println(member.getDOB());

System.out.println(member.getHeight());

System.out.println("---------------");

member.setName("Romeo", "Montague");

member.setDOB(21, 2, 1983);

member.setHeight(73);

System.out.println(member.getName());

System.out.println(member.getDOB());

System.out.println(member.getHeight());

System.out.println("---------------");

member.setName("Triolus", "Montague");

member.setDOB(12, 4, 2009);

member.setHeight(33);

System.out.println(member.getName());

System.out.println(member.getDOB());

System.out.println(member.getHeight());

System.out.println("---------------");

member.setName("Cressida", "Montague");

member.setDOB(13, 10, 2007);

member.setHeight(40);

System.out.println(member.getName());

System.out.println(member.getDOB());

System.out.println(member.getHeight());

}

}

网友答案:

If you want to only change the last name and you don't have a method for that in the Person class you can use getName and save the result. Then you use setName with the old first name and the new last name.

分享给朋友:
您可能感兴趣的文章:
随机阅读: