- package com.itheima;
- public class Demo {
- public static void main(String[] args)
- {
- Person p1= new Person();
- Person p2= new Person("zhangsan", 24);
- System.out.println("p1原"+p1);
- System.out.println("p2原"+p2);
-
- change(p1, new String("王五"));
- change(p2, 28);
- System.out.println("p1新"+p1);
- System.out.println("p2新"+p2);
- }
-
- private static void change(Person p, String name)
- {
- p.setName(name);
- }
- private static void change(Person p, int age)
- {
- p.setAge(age);
- }
- }
- class Person
- {
- Person(){}
- Person(String name, int age)
- {
- this.name= name;
- this.age= age;
- }
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Person [name=" + name + ", age=" + age + "]";
- }
-
- }
复制代码 |