C # Use of classes (Eighth day ),

Source: Internet
Author: User

C # Use of classes (Eighth day ),

The time has come to sum up the knowledge. Today, I learned how to use the class in cloud and College. The following is a summary of my knowledge today:

Theory:

Class Definition Syntax:

[Access modifier] class name

{

Member; // 1. variables used to describe category information through fields

... // 2. Define the action of a class using methods

}

Class can contain the definition and method of variables. Access Modifier

Private: a private member that can be accessed within the class.

Protected: protects members, which can be accessed within the class and in the inheritance class.

Public: A public member. It is completely public and has no access restrictions.

Internal: accessible within the current Assembly.

The default access modifier for a field is Private. The modified field can only be accessed within a class.

Class instantiation, with the keyword new-Syntax: class instance name = new class (); Class member access:

Instance name. Attribute Instance name. Method Name ();

• Fields, methods, and attributes (described later) can all be called Member of the class. Define access level. The purpose of the access level is to control where members can be accessed, so as to "encapsulate" the object.

ClassIt can be instantiated, which belongs to the reference type and is allocated to the memory stack. Class is passed by reference.

StructIt belongs to the value type and is allocated to the memory stack. The struct is transmitted by replication. Object initializer:Class Name. Object Name = new Class Name () {field assignment} Attribute• Public modifier for fields... Use private • attribute definition. get; set; (Set Value assignment, get value)• Attributes are used to protect the corresponding fields and ensure that the Fields read and assigned values meet the requirements. • attributes can be divided into read/write, read-only, and write-only attributes. Variables that allow external access must be declared as attributes. Class Constructor• Constructor is used to create an object and can initialize the object in the constructor. • Constructor is a special method used to create an object. The method name is the same as the class name, and there is no return value, so no void is required. • Constructor can have parameters. When a new object is added, the function parameter can be passed. • If no constructor is specified, the class has a default no-argument constructor. If a constructor is specified, no default constructor is available. If no constructor is required, you need to write the constructor by yourself. • Constructor can be overloaded, that is, there are multiple constructors with different parameters. The constructor can solve the following problems:

1) when initial values are assigned, duplicate object names are written.

2) if we have an attribute that cannot be modified at will, we generally define this attribute as a read-only attribute.

Then the attribute of this read-only type cannot be assigned to him after instantiation. How can we initialize it? We

The constructor can be used for initialization.

We define a class. If no constructor is written, the compiler will automatically add

Constructor with parameters.

Once we write a constructor, the compiler will not add this constructor without parameters.

Practice:

• Exercise to define a human. Humans use three variables to represent the gender and age of names, one way to eat, and one way to exercise. First, add a class in the project and define  Call class

Defines a student class with six attributes: name, gender, age, language, mathematics, and English.

There are two methods: -One greeting method: introduce your name as XX. How old is this year. Is it male or female. -Two methods are used to calculate the total score and average score. {Display: My name is XX. The total score for this test is X, and the average score is X} Implement two objects and test: -Michael Zhang's score for 18 subjects is 90, 95, and 80. -The score for female 16 in three subjects is: 95 85 100.
Class Student {string name; public string Name {get {return name;} set {name = value ;}} string gender; public string Gender {get {return gender ;} set {gender = value ;}} int age; public int Age {get {return age ;}set {age = 18 ;}} int chinese; public int Chinese {get {return chinese;} set {chinese = value ;}} int math; public int Math {get {return math ;}set {math = value ;}} Int english; public int English {get {return english;} set {english = value ;}}// method of greeting: this is the age of XX. Is it male or female. // Two methods are used to calculate the total score and average score. {Display: My name is XX. The total score for this test is X, and the average score is X.} public void SayHi () {Console. writeLine ("My name is {0}. I am {1} years old. I am {2} student", name, age, gender);} public void Score () {int sum = 0; int avg = 0; sum = chinese + math + english; avg = sum/3; Console. writeLine ("My name is {0}. The total score for this test is {1}, and the average score is {2}", name, sum, avg );}}

 

For constructor, continue to summarize and learn about it tomorrow... Come on!

 

 

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.