|NO.Z.00041|——————————|BigDataEnd|——|Java&static关键字继承.V01|——|Java.v01|people类|测试类实现|

一、people类和测试类的实现
### --- 案例题目

~~~     ——>        编程实现People类的封装,特征有:姓名、年龄、国籍,要求提供打印所有特征的方法。
~~~     ——>        编程实现PeopleTest类,main方法中使用有参方式构造两个对象并打印。
二、static关键字的由来
二、编程代码
### --- 编程代码:编程实现People类的封装

/*
    编程实现People类的封装
 */
public class People {
    
    // 1.私有化成员变量,使用private关键字修饰
    private String name;
    private int age;
    private String country;
    
    // 3.在构造方法中调用set方法进行合理值的判断
    public People() {}
    public People(String name, int age, String country) {
        setName(name);
        setAge(age);
        setCountry(country);
    }
    
    // 2.提供公有的get和set方法,并在方法体中进行合理值的判断
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if(age > 0 && age < 150) {
            this.age = age;
        } else {
            System.out.println("年龄不合理哦!!!");
        }
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    
    public void show() {
        System.out.println("我是" + getName() + ",今年" + getAge() + "岁了,来自" + getCountry());
    }
}
三、编程代码:编程实现people类的测试
### --- 编程代码:编程实现people类的测试

/*
    编程实现People类的测试
 */
public class PeopleTest {
    
    public static void main(String[] args) {

        // 1.使用有参方式构造两个People类型的对象并打印特征
        People p1 = new People("zhangfei", 30, "China");
        p1.show(); // zhangfei 30 China
        
        People p2 = new People("guanyu", 35, "China");
        p2.show(); // guanyu 35 China
    }
}
四、编译打印
### --- 编译

C:\Users\Administrator\Desktop>javac PeopleTest.java
### --- 打印输出

C:\Users\Administrator\Desktop>java PeopleTest
我是zhangfei,今年30岁了,来自China
我是guanyu,今年35岁了,来自China

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(8)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示