Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Sử dụng thừa kế này bao gồm các phần thừa kế liên quan đến cú pháp cần thiết mà bạn cần phải hiểu để tạo lớp kế thừa từ các lớp khác. Lớp học cơ sở và lớp học nguồn gốc Cú pháp tuyên bố rằng một lớp kế thừa từ lớp khác là như sau | Using Inheritance This section covers the essential inheritance-related syntax that you need to understand in order to create classes that inherit from other classes. Base Classes and Derived Classes The syntax for declaring that a class inherits from another class is as follows class DerivedClass BaseClass . The derived class inherits from the base class. Unlike other languages such as C in C a class is allowed to derive from at most one other class a class is not allowed to derive from two or more classes. However unless DerivedClass is declared as sealed see the section titled Sealed Classes later in this chapter you can create further derived classes that inherit from DerivedClass using the same syntax class DerivedSubClass DerivedClass . In this way you can create inheritence hierarchies. Suppose you are writing a syntax analyzer as part of a compiler. You need to define a class that represents each of the elements of a program according to the syntax rules of the language. Such elements are sometimes referred to as tokens. You could declare the Token class as shown below. The constructor builds a Token object from the string of characters passed in which could be a keyword an identifier a piece of white space or any other valid piece of syntax for the language being parsed class Token public Token string name . . You could then define classes for each different classification type of token based on the Token class adding additional methods as necessary. For example class IdentifierToken Token . TIP C programmers should note that you do not and cannot explicitly specify whether the inheritance is public private or protected. C inheritance is always implicitly public. Java programmers should note the use of the colon and that there is no extends keyword. Remember that the System.Object class is the root class of all classes. All classes implicitly derive from the System.Object class. For example if you implement the Token class like this class Token public .