JAVA面向对象学习——java面向对象概念———实例化2个对象——this使用理解

public class Employee
{
    String name;
    int age;
    String designation;
    double salary;

    public Employee(String name)
    {
        this.name = name;
    }

    public void empAge(int empAge)
    {
        age =  empAge;
    }

    public void empDesignation(String empDesig)
    {
        designation = empDesig;
    }

    public void empSalary(double empSalary)
    {
        salary = empSalary;
    }

    public void printEmployee()
    {
        System.out.println("名字:"+ name );
        System.out.println("年龄:" + age );
        System.out.println("职位:" + designation );
        System.out.println("薪水:" + salary);
    }

    public static void main(String[] args)
    {

        Employee empOne = new Employee("RUNOOB1");
        Employee empTwo = new Employee("RUNOOB2");


        empOne.empAge(26);
        empOne.empDesignation("gao_ji");
        empOne.empSalary(1000);
        empOne.printEmployee();

        empTwo.empAge(21);
        empTwo.empDesignation("di_ji");
        empTwo.empSalary(500);
        empTwo.printEmployee();
    }


}

  

 

名字:RUNOOB1
年龄:26
职位:gao_ji
薪水:1000.0

 

 

名字:RUNOOB2
年龄:21
职位:di_ji
薪水:500.0

 

 

 

 

 

 

 

 

 

 

 

 

public class Employee
{
    String name;
    int age;
    String designation;
    double salary;

    public Employee(String name,int age)
    {
        this.name = name;
        this.age = age;
    }


    public void empDesignation(String empDesig)
    {
        designation = empDesig;
    }

    public void empSalary(double empSalary)
    {
        salary = empSalary;
    }

    public void printEmployee()
    {
        System.out.println("名字:"+ name );
        System.out.println("年龄:" + age );
        System.out.println("职位:" + designation );
        System.out.println("薪水:" + salary);
    }

    public static void main(String[] args)
    {

        Employee empOne = new Employee("RUNOOB1",25);
        Employee empTwo = new Employee("RUNOOB2",22);


        empOne.empDesignation("gao_ji");
        empOne.empSalary(1000);
        empOne.printEmployee();

        empTwo.empDesignation("di_ji");
        empTwo.empSalary(500);
        empTwo.printEmployee();
    }


}

  

 

 

名字:RUNOOB1
年龄:25
职位:gao_ji
薪水:1000.0

 

名字:RUNOOB2
年龄:22
职位:di_ji
薪水:500.0

上一篇:Spring Boot: 集成Mybatis 返回自增主键, 存储过程调用, 传递多参数,where set trim foreach用法 association和collection嵌套查询 集合查


下一篇:导出文件:使用Hutool导出数据为Excel文件