什么是抽象类? [英] What is an abstract class?

查看:144
本文介绍了什么是抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我得知抽象类是说WT(H *)!

问题:


  1. 什么是不能被实例化创建一个类的点?

  2. 为什么会有人想要这样的类?

  3. 什么是在抽象类成为必需的情况呢?

*的如果你知道我的意思


解决方案

  1. 最常见的,作为一个基类或接口(一些语言有一个单独的接口结构,有的没有) - 它没有知道的实施(即由子类来提供/实现类)

  2. 提取和再利用

  3. 当基类可以提供任何有意义的默认实现的方法(但允许子类重复使用实现的非抽象的部分;任何领域,非抽象方法等)

例如:

 公共抽象类流{/ *批次code,一些抽象方法* /}

到底是什么流的本身?什么样的流的?一个流文件?网络?内存缓冲区?各自可以具有读/写的不同的和不相关的方式,而是提供一个通用的API。它使无感以创建的只是的一个,而是通过摘要类,你可以的 code 的如果不知道的细节 API

 流S = CreateStream(...); //我不小心* *什么样的流
s.Write(新的byte [] {1,2,3,4,5});
S.CLOSE();

When I learned about abstract classes is said WT(H*)!!!

QUESTIONS:

  1. What is the point of creating a class that can't be instantiated?
  2. Why would anybody want such a class?
  3. What is the situation in which abstract classes become NECESSARY?

*if you know what i mean

解决方案

  1. most commonly to serve as a base-class or interface (some languages have a separate interface construct, some don't) - it doesn't know the implementation (that is to be provided by the subclasses / implementing classes)
  2. abstraction and re-use
  3. when the base-class can provide no meaningful default-implementation for a method (but allowing subclasses to re-use the non-abstract parts of the implementation; any fields, non-abstract methods, etc)

For example:

public abstract class Stream { /* lots of code, some abstract methods */ }

What the heck is a stream by itself? What kind of stream? a stream to a file? a network? a memory buffer? Each may have different and unrelated ways of reading / writing, but provide a common API. It makes no sense to create just a Stream, but via the abstract class, you can code to the Stream API without knowing the details:

Stream s = CreateStream(...); // I don't *care* what kind of stream
s.Write(new byte[] {1,2,3,4,5});
s.Close();

这篇关于什么是抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆