类与对象练习

本文通过六个面向对象的案例分析,详细探讨了如何设计和使用类与对象,包括Address类、Employee类、Dog类、银行账号类、User类以及图书类的实现,涵盖了属性定义、构造方法、方法实现以及数据成员的访问控制等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、面向对象案例分析一

1、编写并测试一个代表地址的Address类,地址有国家、省份、城市、街道、邮编等组成。并返回完整的地址信息。

package anli1_;

/**
 * @ClassName : Address  //类名
 * @Description : 国家、省份、城市、街道、邮编等//描述
 * @Author : ***  //作者
 * @Date: 2021-08-01 10:27 //日期
 */

public class Address {
   
    private String country; //国家
    private String province;//省份
    private String city;    //城市
    private String street;  //街道
    private long Zipcode;   //邮编

    public Address(){
     //无参列表

    }
    public Address(String country, String province, String city, String street, long zipcode) {
   
        this.country = country;
        this.province = province;
        this.city = city;
        this.street = street;
        Zipcode = zipcode;
    }

    //setget方法
    public String getCountry() {
   
        return country;
    }

    public void setCountry(String country) {
   
        this.country = country;
    }

    public String getProvince() {
   
        return province;
    }

    public void setProvince(String province) {
   
        this.province = province;
    }

    public String getCity() {
   
        return city;
    }

    public void setCity(String city) {
   
        this.city = city;
    }

    public String getStreet() {
   
        return street;
    }

    public void setStreet(String street) {
   
        this.street = street;
    }

    public long getZipcode() {
   
        return Zipcode;
    }

    public void setZipcode(long zipcode) {
   
        Zipcode = zipcode;
    }

    //返回字符串
    @Override
    public String toString() {
   
        return "Address{" +
                "country='" + country + '\'' +
                ", province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", street='" + street + '\'' +
                ", Zipcode=" + Zipcode +
                '}';
    }
}
package anli1_;

/**
 * @ClassName : testAdderss  //类名
 * @Description : 编写一个测试类 //描述
 * @Author : ***  //作者
 * @Date: 2021-08-01 10:26 //日期
 */

public class testAdderss {
   
    public static void main(String[] args) {
   
        //对象实例化
        Address address = new Address("中国","河北","石家庄","滹沱河",100100);
        //输出
        System.out.println(address.toString());
    }
}
程序输出结果:
Address{
   country='中国', province='河北', city='石家庄', street='滹沱河', Zipcode=100100}

二、面向对象案例分析二

2、定义并测试代表员工的Employee类,员工属性包括编号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值