(⾯向对象基础)写⼀个Worker 类,并创建多个Worker 对象

    技术2025-03-04  34

    1 为Worker 类添加三个属性:

    1.1 String 类型的name,表示⼯⼈的姓名。 1.2 int 类型的age,表示⼯⼈的年龄。 1.3 double 类型的salary,表示⼯⼈的⼯资。

    2 为Worker 类添加两个构造⽅法: 2.1 公开⽆参构造⽅法。 2.2 接受三个参数的构造⽅法,三个参数分别为字符串、int 和double 类型。

    3 为Worker 类添加两个work ⽅法: 3.1 ⽆参构造⽅法 3.2 带整数参数构造⽅法,表示⼯⼈⼯作的时间(为多少⼩时)

    public class Test1 { public static void main(String[] args) { Worker w1 = new Worker(); w1.name = "zhu"; w1.age = 1; w1.salary = 2000; System.out.print(w1.name + "的年龄为:" + w1.age + ",工资为" + w1.salary); System.out.println(); Worker w2 = new Worker(); System.out.println("-----------------------------"); w2.name = "zhuzhu"; w2.age = 66; w2.salary = 30000; System.out.println(); System.out.print(w2.name + "的年龄为:" + w2.age + ",工资为" + w2.salary); System.out.println(); new Worker(); System.out.println("-----------------------------"); new Worker("你好", 100,5000); System.out.println("-----------------------------"); Worker w3 = new Worker(); w3.work(); System.out.println(w3.work(8)); } } class Worker { String name; int age; double salary; public Worker() { System.out.println(50); } public Worker(String a,int b,double c) { System.out.println(a); System.out.println(b); System.out.println(c); } public void work(){ System.out.println("这是work实例方法!"); } public int work(int hours) { return hours; } }
    Processed: 0.011, SQL: 9