Discuss / Java / 练习

练习

Topic source
public class Main {
    public static void main(String[] args) {
        Person ming = new Person();
        Person hong = new Person();
        ming.setName("Xiao Ming");
        // TODO: 给Person增加重载方法setName(String, String):
        hong.setName("Xiao", "Hong");
        System.out.println(ming.getName());
        System.out.println(hong.getName());
    }
}
class Person {
	private String name;
    //一个字符串组成的名字
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {   	
    	return this.name;
    }
        
    //姓+名
    public void setName(String xing,String ming) {
    	name = xing +" "+ ming;
    }
    
}

  • 1

Reply