java组合与继承始示例_Java示例中的组合

java组合与继承始示例

Composition in java is the design technique to implement has-a relationship in classes. We can use java inheritance or Object composition in java for code reuse.

Java中的组合是在类中实现has-a关系的设计技术。 我们可以在Java中使用Java继承或Object组合进行代码重用。

用Java编写 (Composition in Java)

Java composition is achieved by using instance variables that refers to other objects. For example, a Person has a Job. Let’s see this with a java composition example code.

Java组合是通过使用引用其他对象的实例变量来实现的。 例如,一个Person有一个Job 。 让我们用Java合成示例代码来看看。

Java撰写范例 (Java Composition Example)

package com.journaldev.composition;

public class Job {
    private String role;
    private long salary;
    private int id;
        
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public long getSalary() {
        return salary;
    }
    public void setSalary(long salary) {
        this.salary = salary;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    
    
}
package com.journaldev.composition;

public class Person {

    //composition has-a relationship
    private Job job;
   
    public Person(){
        this.job=new Job();
        job.setSalary(1000L);
    }
    public long getSalary() {
        return job.getSalary();
    }

}

Here is a test class for java composition example that uses person object and get it’s salary.

这是一个使用person对象并获取薪水的java合成示例的测试类。

package com.journaldev.composition;

public class TestPerson {

    public static void main(String[] args) {
        Person person = new Person();
        long salary = person.getSalary();
    }

}

Java组合的好处 (Java Composition Benefits)

Notice that above test program for composition in java is not affected by any change in the Job object. If you are looking for code reuse and the relationship between two classes is has-a then you should use composition rather than inheritance.

请注意,上述用于Java编写的测试程序不受Job对象中任何更改的影响。 如果您正在寻找代码重用并且两个类之间的关系是has-a,那么您应该使用组合而不是继承。

Benefit of using composition in java is that we can control the visibility of other object to client classes and reuse only what we need.

在Java中使用组合的好处是,我们可以控制其他对象对客户端类的可见性,并且仅重用我们需要的对象。

Also if there is any change in the other class implementation, for example getSalary returning String, we need to change Person class to accommodate it but client classes doesn’t need to change.

同样,如果其他类实现中有任何更改,例如getSalary返回String,则我们需要更改Person类以适应它,但是客户端类不需要更改。

Composition allows creation of back-end class when it’s needed, for example we can change Person getSalary method to initialize the Job object at runtime when required.

组合允许在需要时创建后端类,例如,我们可以更改Person getSalary方法以在需要时在运行时初始化Job对象。

Further Reading: Do you know one of the best practice in java programming is to use composition over inheritance, check out this post for detailed analysis of Composition vs Inheritance.

进一步阅读:您是否知道Java编程中的最佳实践之一是使用组合而不是继承,请查看这篇文章以详细分析“ 组合vs继承”

Java Composition YouTube视频 (Java Composition Youtube Video)

Recently I created a YouTube video to explain composition in java in detail, please watch it below.

最近,我创建了一个YouTube视频,以详细说明Java的组成,请在下面观看。

演示地址

That’s all for composition in java or java composition example. I hope you will find it useful in making informed decision when designing your application classes.

Java或Java合成示例中的全部内容。 我希望您会发现在设计应用程序类时做出明智的决定很有用。

Reference: Wikipedia Page

参考: 维基百科页面

翻译自: https://www.journaldev.com/1325/composition-in-java-example

java组合与继承始示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值