集册 Java实例教程 测验定义自定义类

测验定义自定义类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

485
创建一个使用访问控制的ZipCode类,以确保其zipCode实例变量始终具有五位数字的值。
/*
 from N o w J a v a . c o m 
*/

public class Main{


    public static void main(String[] arguments) {

        ZipCode zip1 = new ZipCode("444344");

        ZipCode zip2 = new ZipCode("111110");

        if (zip1.getZipCode() != "") {

            System.out.println("Zip1: " + zip1.getZipCode());

        }

        if (zip2.getZipCode() != "") {

            System.out.println("Zip2: " + zip2.getZipCode());

        }

    }

}


class ZipCode {

    private String zipCode = "";/** from 时   代    Java - nowjava.com**/


    public ZipCode(String inZipCode) {

        setZipCode(inZipCode);

    }


    public void setZipCode(String inZipCode) {

        if (
展开阅读全文