Black Horse programmer 02-Relationship between classes and objects

Source: Internet
Author: User

--- Java training, Android training, IOS training, and. Net training. We look forward to communicating with you! ---

1. How to create an object

1) how to create an object if an object is required to solve the problem?

2) real-life example: How to Create a car object?

A) first, we need to have a drawing of the car, which describes the attributes and functions (Actions) that the car should possess)

Attribute: Number of wheels, speed

Function (behavior): Run

B) Create a car based on the description on the drawing.

C) each vehicle has its own attributes and functions, and is an example of drawing.

D) drawings are abstract and vehicles are specific. drawings are highly generalization of vehicle objects.

2. Object-oriented in oC

1) classes in OC are equivalent to drawings used to describe a class of things. To create an object, you must first have a class.

2) OC uses classes to create objects. Objects exist in classes and classes are abstract descriptions of objects.

3) Therefore, to solve the problem through object-oriented methods, we should first consider the classes to be designed and how many objects to create using classes.

 

3. What classes are needed and how to design classes?

(1) the class design requires three elements to create a class. The naming rules of the class are as follows:

    • Class Name: The first letter must be in upper case. The following words follow the hump principle.
    • Attribute: It generally starts with an underscore. The following words follow the hump principle.
    • Behavior (function): Follow the hump Principle

(2) General nouns are all Class

A) The tank fired three shells and fired two planes.

Tanks, shells, and airplanes

B) James is holding a dog with a hot dog on the bus.

Buses, hot dogs, and dogs are all similar. James is a human.

(3) objects with the same (or similar) attributes and behaviors can abstract a class.

(4) Class Name, attribute, and behavior exercise

Aircraft

Class Name: Plane

Attribute: color (_ color), model (_ Model)

Behavior: Fly)

 

Human

Class Name: person

Attribute: height (_ height), weight (_ weight)

Behavior: Run and eat)

 

Apple mobile

Class Name: iPhone

Attribute: color (_ color), model (_ model), CPU (_ CPU)

Behavior: lookmyphone, callphone, sendmessage, and receiptmessage)

(5) which object knows the behavior the most, and writes the behavior to the object.

For example, if you enable a computer, it should belong to the computer.

4. design exercises

(1) Class Design:

1> Class Name

* The first letter of the class name must be in uppercase.

* Underlines are not allowed (errors are not allowed, but norms are not allowed)

* Multiple English words, following the hump Principle

2> attributes

2> behavior (function)

 

(2) Plants vs. botnets

* Class Name: zoombie (zombie)

* Botnets: life, speed, and gongjili)

* Behavior: Walk, bite, and die)

 

(3) lightning planes

* Class Name: plane (Aircraft)

* Attributes: life, gongjili, speed, and bombcount)

* Behavior: Fly, bomb, shoot, and die)

 

(4) Computers

* Class Name: Computer)

* Attributes: band (brand) and expiredate (shelf life)

* Action: open (BOOT), close (shutdown)

 

This section focuses on:

1. The object is the specific existence of the class, and the class is the abstract description of the object.

2. To solve the problem through object-oriented methods, we should first consider which classes should be designed and how many objects should be created using classes.

3. Class Design: (3 elements)

1> Class Name

* The first letter of the class name must be in uppercase.

* Underlines are not allowed (errors are not allowed, but norms are not allowed)

* Multiple English words, following the hump Principle

2> attributes

* It generally starts with a line below, and the following words follow the hump principle.

2> behavior (function)

* Follow the hump Principle

Black Horse programmer 02-Relationship between classes and objects

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.