Properties of encapsulation and classes classroom on-machine Exercise
Classroom task One: definition and use of the field
Time: 12 minutes
Required:
Creates a field in car class and displays information for each field in the button's Click event.
Step:
? 1. Create a project of a form type Carsystem
? 2, create car class cars, this class contains the fields: car name, price, manufacturers
? 3, write a button click event, use the MessageBox.Show method to display these field information
? 4, create a form, enter the car name, price, manufacturer, and store it in the corresponding field in the Car object
? 5, Test the class, try the price of 1 billion yuan will be wrong? Does this price accord with the common sense of life?
Classroom Task Two: definition and use of properties
time: 10 minutes
Requirements:
Creates an attribute for the Price field in the previous car class car, with a rule of a price greater than 20,000 and less than 50 million.
Step:
1, create a property for the price field of the car class, the rule is that the price is greater than 20,000 and less than 50 million
2, test the class, the attempt price is 1 billion yuan error?
Classroom Task Three: Declaration and use of methods
Time: 12 minutes
Requirements:
in front of car cars, use the method showcarinfo to display information for each field.
Step:
1, write a method showcarinfo in the class car, and messagebox.show the information of each field in the method, no return value is required.
2, add a button, call Showcarinfo display information in its Click event,
3, test the method is correct
4, [supplemental] Another method ShowCarInfo2, return the field information to be displayed. (done)
Classroom Task Four: Writing construction methods for classes
Time: 12 minutes
Requirements:
Create a constructor for car cars and initialize each field with the values in the form text box.
steps:
1, create a constructor for car cars, and initialize each field with the values in the form text box.
2, add a button to create a car object in its Click event, initialize the fields, and then call Showcarinfo to display the information.
3. The test initialization data is correct.
Properties for C # encapsulation and classes classroom on-machine exercises