Is-a、has-a、Like-a区别

Is-a:

是a:A Is B:A是B(继承关系,继承)。

has-a:

有a:A has B:A有B(从属关系,聚合)。

like-a:

像a:A like B:A像B(组合关系,接口)。

关于Is-a、has-a、like-a的使用场景:

如果A,B是Is-a关系,那么应该使用继承,例:玻璃杯、塑料杯都是杯子。

如果A,B是has-a关系,那么应该是用聚合,例:汽车由发动机,底盘,车身,电气设备等组成,那么应该把发动机,底盘这些类聚合成汽车。

如果A,B是like-a关系,那么应该使用组合,例:空调继承于制冷机,但它同时有加热功能,那么你应该把让空调继承制冷机类,并实现加热接口。


抽象类与其派生类是一种“is-a”关系,即父类和派生子类在概念上的本质是相同的(父子关系,血缘联系,很亲密)。 

接口与其实现类是一种“like-a”关系,即接口与实现类的关系只是实现了定义的行为,并无本质上的联系(契约关系,很淡漠的利益关系)。

举个例子:比如说一个动物抽象类,定义了跑的方法、叫的方法,但如果一个汽车类可以实现跑、可以实现叫,它就可以继承动物抽象类吗?!这太不合理了,汽车不是动物呀!而如果通过接口定义跑的方法、叫的方法,汽车类作为实现类实现跑和叫,完全OK很合理,就因为没有继承关系的约束。


注:以上文章仅是个人学习过程总结,若有不当之处,望不吝赐教。

C# is a versatile and powerful programming language that has evolved significantly since its introduction. It offers a wide array of features that make it a compelling choice for developers. Below are some key features of the C# programming language, with comparisons to similar features in other languages. ### Object-Oriented Programming C# supports object-oriented programming (OOP) principles, including encapsulation, inheritance, and polymorphism. Classes and objects are fundamental to C# and allow for the creation of modular programs and reusable code[^3]. Similar to Java, C# uses a class-based approach to OOP, where classes are used to define the structure and behavior of objects. ### Type Safety C# is a type-safe language, which means that the compiler ensures that operations are performed on compatible data types. This helps prevent many common programming errors that can occur when working with incompatible types. In contrast, languages like C++ offer more flexibility but at the cost of type safety. ### Garbage Collection C# employs automatic memory management through garbage collection, which helps manage the allocation and release of memory resources. This feature is similar to Java and is in contrast to languages like C and C++, where developers must manually manage memory allocation and deallocation. ### LINQ (Language Integrated Query) C# introduced LINQ, which allows developers to query data directly within the language using a SQL-like syntax. This feature simplifies data manipulation and is integrated into the language itself, making it more intuitive and easier to use compared to external query languages[^2]. ### Partial Classes and Methods C# 2.0 introduced partial classes and methods, allowing the definition of a class to be split across multiple files. This feature is particularly useful in large projects where multiple developers may be working on different parts of the same class simultaneously[^2]. ### Delegates and Events C# supports delegates and events, which are used to implement event-driven programming. Delegates are similar to function pointers in C++ but are type-safe and secure. Events are a special kind of delegate that can be used to notify other objects of changes or actions. ### Generics C# supports generics, which allow for the creation of type-safe collections and methods that can work with any data type. This feature is similar to templates in C++ but provides better type safety and performance. ### Asynchronous Programming C# includes support for asynchronous programming through the `async` and `await` keywords, which simplify the process of writing non-blocking code. This feature is comparable to JavaScript's Promises and async/await syntax. ### Nullable Value Types C# allows value types to be nullable, which means that they can represent the absence of a value. This feature is useful when working with databases or other data sources that may contain null values. ### Code Snippet Example Here is a simple example demonstrating the use of a class in C#: ```csharp public class Person { public string Name { get; set; } public int Age { get; set; } public void Introduce() { Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old."); } } // Usage Person person = new Person { Name = "Alice", Age = 30 }; person.Introduce(); ``` This code defines a `Person` class with properties `Name` and `Age`, along with a method `Introduce` that prints a message to the console. ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值