javase练习题6

成员变量基础练习

第1题

案例:

​ 声明员工类Employee,包含属性:编号、姓名、年龄、薪资,

​ 声明Test01测试类,并在main方法中,创建2个员工对象,并为属性赋值,并打印两个员工的信息。

public class Employee {
   

    private int number;
    private String name;

    private int age;

    private double salary;
    public Employee(){
   


    }

    public  Employee(int number,String name,int age,double salary){
   

        this.number=number;
        this.name=name;
        this.age=age;
        this.salary=salary;
    }

    public void setAge(int age) {
   

        this.age = age;
    }

    public int getAge() {
   
        return age;
    }

    public void setNumber(int number) {
   

        this.number = number;
    }
    public int getNumber(){
   
        return number;
    }

    public void setName(String name){
   

        this.name=name;
    }
    public String getName(){
   
        return name;

    }

    public void setSalary(double salary){
   

        this.salary=salary;
    }
    public double getSalary(){
   

        return salary;
    }
    public void printInfo(){
   

        System.out.println("编号是:"+number+",年龄是:"+age+"姓名是:"+name+"薪资是:"+salary);
    }
}

package com.cwbjava.homework.homework_6.question_1;

public class Test {
   
    public static void main(String[] args) {
   

        Employee employee = new Employee();
        employee.setName("李白");
        employee.setAge(18);
        employee.setNumber(1);
        employee.setSalary(2345.34);
        employee.printInfo();

        Employee employee1 = new Employee(2, "李清照", 18, 265.9);
        employee1.printInfo();

    }
}

第2题

案例:

​ 声明一个日期类MyDate,包含属性:年、月、日

​ 声明一个Test02测试类,并在main方法中,创建3个日期对象,一个是你的出生日期,一个是毕业的日期,并打印显示

public class MyDate {
   
    private int year;
    private int  month;
    private int day;

    public MyDate(int year, int month, int day) {
   
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public MyDate() {
   
    }

    public int getYear() {
   
        return year;
    }

    public void setYeat(int yeat) {
   
        this.year = yeat;
    }

    public int getMonth() {
   
        return month;
    }

    public void setMonth(int month) {
   
        this.month = month;
    }

    public int getDay() {
   
        return day;
    }

    public void setDay(int day) {
   
        this.day = day;
    }

    @Override
    public String toString() {
   
        return "MyDate{" +
                "year年=" + year +
                ", month月=" + month +
                ", day=日" + day +
                '}';
    }
}

public class Test1 {
    public static void main(String[] args) {
   

MyDate m1 = new MyDate();
    m1.setYeat(1997);
    m1.setMonth(6);
    m1.setDay(14);
    System.out.println(m1);
    MyDate m2 = new MyDate(1997,3,14);
    System.out.println(m2);


	}
}
public class Test1 {
   
    public static void main(String[] args) {
   

        MyDate m1 = new MyDate();
        m1.setYeat(1997);
        m1.setMonth(6);
        m1.setDay(14);
        System.out.println(m1);
        MyDate m2 = new MyDate(1997,3,14);
        System.out.println(m2);



    }
}

第3题

案例:

​ 声明公民类Citizen,包含属性:姓名,生日,身份证号,其中姓名是String类型,生日是MyDate类型,身份证号也是String类型。

​ 声明Test03测试类,在main方法中创建你们家庭成员的几个对象,并打印信息。

public class MyDate {
   
    private int year;
    private int  month;
    private int day;

    public MyDate(int year, int month, int day) {
   
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public MyDate() {
   
    }

    public int getYear() {
   
        return year;
    }

    public void setYeat(int yeat) {
   
        this.year = yeat;
    }

    public int getMonth() {
   
        return month;
    }

    public void setMonth(int month) {
   
        this.month = month;
    }

    public int getDay() {
   
        return day;
    }

    public void setDay(int day) {
   
        this.day = day;
    }

    @Override
    public String toString() {
   
        return "MyDate{" +
                "year年=" + year +
                ", month月=" + month +
                ", day=日" + day +
                '}';
    }
}
/**
 * ​	声明公民类Citizen,包含属性:姓名,生日,身份证号,其中姓名是String类型,生日是MyDate类型,
 *      身份证号也是String类型。
 * ​	声明Test03测试类,在main方法中创建你们家庭成员的几个对象,并打印信息。
 */

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值