继承(下)2-6检查
来源:2-6 编程练习
aming41
2020-05-22 19:35:54
//测试类 public class Test { public static void main(String[] args) { //实例化对象,传入属性值(李明, 男,18) Person p = new Person("李明", 18, "男"); //打印输出对象信息 System.out.println(p); System.out.println(p.toString()); } } public class Person{ //私有属性:name(姓名)、age(年龄)、sex(性别) private String name; private int age; private String sex; public Person(){ } //带参构造方法(name、age、sex为参数) public Person(String name, int age, String sex){ this.name = name; this.age = age; this.sex = sex; } //通过封装实现对属性的get/set方法设定 public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public void setAge(int age){ this.age = age; } public int getAge(){ return this.age; } public void setSex(String sex){ this.sex = sex; } public String getSex(){ return this.sex; } //重写toString方法,表示形式为:姓名:+**+ 年龄:+**+ 性别:+** public String toString(){ return "姓名:" + this.getName() + " 年龄:" + this.getAge() + " 性别:" + this.getSex(); } }
1回答
好帮手慕小尤
2020-05-23
同学你好,已完成练习,棒棒哒!继续加油!
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题