C # for Unity programming language Quick Start tutorial (serial 8)---Abstract classes and interfaces for C#oop programming

Source: Internet
Author: User
Tags modifiers

C # Abstract classes and interfaces, which are very important concepts in c#oop programming, play an important role in the development of "high cohesion, low coupling" excellent projects.

C # Abstract classes are represented by the keyword abstract, which can be used to modify classes (abstract classes) or to modify methods (abstract methods). The main function of abstract class is to enumerate the behaviors required by a class, provide a series of stipulations, and constrain the behavior of subclasses.


C # Abstract classes and abstract methods have the following important rules:

1: Abstract classes cannot be instantiated.

2: Abstract classes do not explicitly provide a concrete method implementation. (but can contain normal methods)

3: Defines the abstract method in the base class (the parent Class), the derived class (subclass) must overload (override) the method.
4: Abstract methods must belong to abstract classes, but abstract classes do not necessarily include abstract methods.
5: Abstract methods cannot be private, and the access restrictions for abstract methods and their overloaded methods should be the same.
6: Abstract methods cannot be decorated with the virtual keyword.
7: Abstract classes cannot be sealed or static.

8: An abstract class must implement its abstract method by its subclasses, unless the subclass is also an abstract class.

9: Inheriting derived classes of abstract classes, you must use override to implement an abstract method.

10: Abstract methods must belong to abstract classes, but abstract classes do not necessarily include abstract methods.


Our life interface is: "Different physical devices and software programs to achieve communication and data transmission specifications and standards" (for example: USB interface, that can be applied to the mouse link, can also be applied to the mobile hard disk, USB disk, mobile phone, digital camera, charging po .... )
The interface in C # is "a statement of some of the features provided externally", and we use the interface keyword to declare an interface, noting that the interface is a programming unit "parallel" to the class, with no affiliation to each other.


C # interfaces have the following important rules:

1: Interfaces can inherit, similar to inheritance of classes.

2: Interfaces can implement multiple inheritance, classes are not available.

3: The interface can be understood as a more pure abstract class, because all the methods in the interface are abstract, there is no ordinary method.

4: Interfaces and abstract classes cannot be instantiated.

5: Any derived implementation class for an interface must implement all methods of the interface.

6: The interface does not allow any constructors or fields.

7: Interface members are always public and cannot be declared virtual and static.

8: No access modifiers are allowed in the interface, (implicitly public) No access modifiers are required.

9: The class does not allow multiple inheritance, but the interface allows multiple inheritance.
Good programming habits: Use the interface name as the prefix for the method, which is called "Display interface implementation."
For methods that explicitly specify an interface, it is no longer permissible to use modifiers, all public.


The above two articles we have learned methods of overloading, method rewriting, abstract classes, abstract methods, interfaces and so on. Now summarize some of the points of difference between the concepts:

1: You cannot use both virtual and override to modify a method.
2: The virtual method cannot be private.
3: Understanding of Interface, virtual, override, sealed keywords:
Interface: Introduce the name of a method
Virtual: The first implementation of a method.
Override: Another implementation of the method.
Sealed: The last implementation of the method.


Currently on C # BASIC programming, as of this author provides 8 technical introductory articles, now provides a class of access modifiers to the current interface of some typical learning exercises for C # beginners to learn.

Programming Topics:

1: Learn to define classes and access modifiers.
1.1&gt: The known transport class is defined as follows.
Class Vehicle
{
private int _length; Length
private int _width; Width

public void Drive () {}; Driving method
}
Requirements: (1) to implement a set of classes, (2) to define and implement a car class car, is its derived class, the car itself, the private properties of the number of vehicles, the method of the car has init (set the number of wheels, weight and number of people), get passenger (to get the number of manned), print (Number of Weight and number of manned).


2: Learning to inherit
2.1&gt: Write a data entry and display program for students and teachers, with numbers, names, class numbers, and scores, with numbers, names, titles, and departments for the data. The number, name input and display are required to be designed as a class person, and as a base class for student data manipulation classes student and teacher data manipulation classes teacher.

3: Method overloading (static polymorphism)
3.1&gt: Using polymorphism to achieve multi-function calculators:
A) Define the method named Computemethod to implement the adder.
B) The method named Computemethod is re-defined, and the subtraction integrated computing function is realized.
C) Call the above method in the main method, and test.
3.2&gt: The use of polymorphism to achieve the following functions.
A) define a father class that defines three construction methods using the overloaded concept of a construction method.
B) define the child class to inherit the Father class, calling the different constructor methods of the parent class, respectively, in the child's constructor method.

4: Method overrides
4.1>: Design a base class in which to create a method Mconvert, which takes a parameter representing the number of kilometers, converts it to the equivalent number of miles, and then creates a subclass that inherits this class, adding a new method Kconvert that converts the number of kilograms entered into points, Finally, the instance object of the subclass is generated, and the two functions are tested.
Tip Information:
1-kilometer (km) = 0.62 miles
1 kilograms (kg) = 2.2 lbs


4.2>: Adjusts the contents of the previous question, declares the Mconvert method in it as virtual, and then overwrite it in the subclass, with its accepted parameters as a square edge length, converted to a mile after the area is calculated.


4.3>: Create a class that overrides the ToString () method, which, when referenced, can output the descriptive text of such an object as follows: "MyObject class object for testing"


5: Abstract class, abstract method
5.1&gt: Write a program to find the area and perimeter of a square and a circle that are inside a circle. Requires the design of abstract base classes, abstract methods, and the use of dynamic polymorphism.
Tip Information:
Formula: Tick definition: Right triangle squared on both sides of the square and equals the square of the third side: A*a+b*b=c*c
The radius of the circle uses R:
Side length =r*r+r*r=x*x of the inner tangent square
X=SQRT (2 (r*r))
Inner Tangent Square Area
X*x
Inner Tangent square Perimeter
4x
Area of the circumscribed square
(r*2) * (r*2)
Perimeter of the circumscribed square
(r*2) *4=8r

Shapes: Shape
Circle: Circle
Square: Square,
Size: area.
Circumference: Length.

5.2>: The shape class below is an abstract class that represents a shape, and area () is a function that plots the areas of the graph. Derive the Ladder class (trapezoid), Circle Class (circle), Triangle Class (Triangle) from the shape class, and give specific area functions. Among them, all derived classes are required to calculate the area of the parameters are given by the constructor, trapezoidal area calculation requires a bottom, bottom and high, triangular area needs a bottom and high, circular area needs radius.
The abstract class of the shape is declared as follows:

Abstract class Shape {
Abstract public double area ();
}


6: interface, multiple interfaces

6.1>:A) defines the Calculate interface,

The interface files are as follows:
Interface Calculate
{
void Getarea (); Calculates the circle area.
void Getzc (); Calculate Circle Perimeter
}

B) define the Circularity class to implement the interface calculate. And the result is output after debugging.

6.2>: Design a class Tclass, inherit the following interface Imeasure, implement the length () and area () method, to calculate the length of the equilateral triangle side length and areas.
Interface Imeasure
{
int Length (int s);
int area (int s);
}

6.3>, design a test program, create an instance object of Tclass, call the method in which the edge length of the triangle area and perimeter of 10 is calculated.

6.4&gt: On the topic, design a test program, create an instance object of Tclass, transform it into a imeasure type, and call the method in which to calculate the area and perimeter of the triangle with an edge length of 10.

6.5>: In addition to the design of a new class, while inheriting the following interface Iameasure and the 2nd above the imeasure, in addition to providing a specific length of the equilateral triangle side length and the area of the calculation method, but also realized the iameasure of areas () members, Provides a method for calculating the square area.
Interface Iameasure
{
int area (int s);
}


This article is from "Teacher Liu Speak Unity" blog, please be sure to keep this source http://liuguozhu.blog.51cto.com/9142031/1831631

C # for Unity programming language Quick Start tutorial (serial 8)---Abstract classes and interfaces for C#oop programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.