|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 2022-04-03 17:45  yanqi_vip  阅读(12)  评论(0)    收藏  举报

导航