• 第九章 原型模式


    大概是java版本升级了,clone方法不严格写好像也没什么问题。

    /**
     * Created by hero on 16-4-2.
     */
    public class Company implements Cloneable {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Company(String name) {
            this.name = name;
        }
    
        public Company clone() {
            Company company = null;
            try {
                company = (Company) super.clone();
            } catch (CloneNotSupportedException e) {
            }
            return company;
        }
    }
    /**
     * Created by hero on 16-4-2.
     */
    public class BusinessCard implements Cloneable {
        private String name;
        private Company company;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Company getCompany() {
            return company;
        }
    
        public void setCompany(Company company) {
            this.company = company;
        }
    
        public BusinessCard clone() {
            BusinessCard card = null;
            try {
                card = (BusinessCard) super.clone();
                if(company!= null){
                    card.company = company.clone();
                }
            } catch (CloneNotSupportedException e) {
            } //won't happen
            return card;
        }
    }
    public class Main {
    
        public static void main(String[] args) {
            BusinessCard card = new BusinessCard();
            card.setName("张无忌");
            card.setCompany(new Company("武当"));
            BusinessCard card1 = card.clone();
            card1.setCompany(new Company("少林"));
            System.out.println(card.getName() + ", " + card.getCompany().getName());
            System.out.println(card1.getName() + ", " + card1.getCompany().getName());
        }
    }
  • 相关阅读:
    安卓 Context 和 Application的关系
    Android Intent应用
    android launchmode(四种启动模式)应用场景及实例
    返回数据给上一个活动
    Intent传参数
    安卓activity生命周期
    如何将nideshop部署到本地
    navicat链接数据库错误2013
    数据库设计三大范式
    nodejs版本升级
  • 原文地址:https://www.cnblogs.com/littlehoom/p/5348016.html
Copyright © 2020-2023  润新知