Java-创建Person类,包含姓名、年龄、性别属性,可查看属性和属性值

时间:2025-04-02 08:10:10
package com.edu.jsu; public class Person { //创建一个类,类名为Person private String name;//声明姓名 private int age;//声明年龄 private String gender;//声明性别 public Person(String name, int age, String gender) { //构造方法,用于创建Person对象时初始化姓名年龄性别属性 this.name = name; this.age = age; this.gender = gender; } public String getName() { //访问方法,用于获取姓名属性值 return name; } public int getAge() {//访问方法,用于获取年龄属性值 return age; } public String getGender() {//访问方法,用于获取性别属性值 return gender; } public void seeInfo() { //方法,用于查看所有属性和属性值 System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Gender: " + gender); } public static void main(String[] args) { Person person1 = new Person("Jane", 16, "Female"); Person person2 = new Person("Mark", 17, "Male"); Person person3 = new Person("LiHua",18,"male"); person1.seeInfo(); person2.seeInfo(); person3.seeInfo(); } }