Java类和对象

2019-07-30  本文已影响0人  spades_K

People.java

public class People {
    String name;
    int age;
    String designation;
    double salary;
    // 构造器
    public People(String name){
        this.name = name;
    }

    // 设置age
    public void empAge(int empAge){
        age = empAge;
    }

    // 打印people信息
    public void  printPeople()
    {
        System.out.println("名字" + name +"年龄" + age);
    }
}

PeopleTest.java

import java.io.*;
public class PeopleTest {
    public void testPeople(){
        // 使用便利构造器初始化
        People p1 = new People("p1");
        People p2 = new People("p2");
        // 赋值age
        p1.empAge(12);
        p2.empAge(88);

        p1.printPeople();
        p2.printPeople();
    }
}

Main.java

import java.io.*;
public class Main {

    public static void main(String[] args) {
        PeopleTest test1 = new PeopleTest();
        test1.testPeople();
    }
}

log
名字p1年龄12
名字p2年龄88

上一篇下一篇

猜你喜欢

热点阅读