定义一个类 Person; 2) 在类中定义属性: 姓名、性别、年龄、学历、身高。
时间: 2024-06-03 16:08:39 浏览: 144
class Person:
def __init__(self, name, gender, age, education, height):
self.name = name
self.gender = gender
self.age = age
self.education = education
self.height = height
相关问题
对人类属性进行封装:1)定义一个类 Person; 2) 在类中定义属性: 姓名、性别、年龄、学历、身高。 3)对属性实现封装,并编写各属性的读写访问器。对属性中的年龄进行控制,要求年龄大于 0,否则使用默认值代替 (年龄默认 18)。 4)在类中定义 show (,功能为输出属性信息。5)在main 方法中实例化2个对象,通过读写访问器完成对属性的赋值和输出操作
class Person:
def __init__(self, name='', gender='', age=18, education='', height=0):
self.__name = name
self.__gender = gender
self.__age = age if age > 0 else 18
self.__education = education
self.__height = height
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
def get_gender(self):
return self.__gender
def set_gender(self, gender):
self.__gender = gender
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age if age > 0 else 18
def get_education(self):
return self.__education
def set_education(self, education):
self.__education = education
def get_height(self):
return self.__height
def set_height(self, height):
self.__height = height
def show(self):
print(f'Name: {self.__name}\nGender: {self.__gender}\nAge: {self.__age}\nEducation: {self.__education}\nHeight: {self.__height}')
person1 = Person()
person1.set_name('Tom')
person1.set_gender('Male')
person1.set_age(25)
person1.set_education('Bachelor')
person1.set_height(180)
person2 = Person('Lucy', 'Female', 30, 'Master', 165)
person1.show()
print()
person2.show()
1)定义一个类 Person; 2)在类中定义属性:姓名、性别、年龄、学历、身高。 3)对属性实现封装,并编写各属性的读写访问器。对属性中的年龄进行控制,要求年龄大于0,否则使用默认值代替(年龄默认 18)。 4)在类中定义show (),功能为输出属性信息。 5)在main方法中实例化2个对象,通过读写访问器完成对属性的赋值和输出操作。
1) class Person {
2) private String name;
3) private String gender;
4) private int age;
5) private String education;
6) private double height;
7)
8) public String getName() {
9) return name;
10) }
11) public void setName(String name) {
12) this.name = name;
13) }
14) public String getGender() {
15) return gender;
16) }
17) public void setGender(String gender) {
18) this.gender = gender;
19) }
20) public int getAge() {
21) return age;
22) }
23) public void setAge(int age) {
24) if(age > 0) {
25) this.age = age;
26) } else {
27) this.age = 18;
28) }
29) }
30) public String getEducation() {
31) return education;
32) }
33) public void setEducation(String education) {
34) this.education = education;
35) }
36) public double getHeight() {
37) return height;
38) }
39) public void setHeight(double height) {
40) this.height = height;
41) }
42)
43) public void show() {
44) System.out.println("姓名:" + this.name);
45) System.out.println("性别:" + this.gender);
46) System.out.println("年龄:" + this.age);
47) System.out.println("学历:" + this.education);
48) System.out.println("身高:" + this.height);
49) }
50) }
51)
52) public class Main {
53) public static void main(String[] args) {
54) Person p1 = new Person();
55) p1.setName("张三");
56) p1.setGender("男");
57) p1.setAge(25);
58) p1.setEducation("本科");
59) p1.setHeight(180);
60) p1.show();
61)
62) Person p2 = new Person();
63) p2.setName("李四");
64) p2.setGender("女");
65) p2.setAge(-5);
66) p2.setEducation("硕士");
67) p2.setHeight(170);
68) p2.show();
69) }
70) }
阅读全文