classes in Java
Class can be viewed as a template for creating Java objects.
Understand the definition of classes in Java by following a simple class: public class Dog {String breed; int age; String color; void Barking () {} void Hungry () {} void Sleeping () {}}}
A class can contain the following types of variables: Local variables: Variables defined in a method, construct method, or statement block are called local variables. Variable declarations and initializations are in the method, and when the method is finished, the variable is automatically destroyed. Member variables: Member variables are variables that are defined outside the method body in the class. This variable is instantiated when the object is created. Member variables can be accessed by methods, construction methods, and statements blocks of specific classes in the class. Class variables: Class variables are also declared in the class, outside the method body, but must be declared as a static type.
A class can have multiple methods, in the example above: Barking (), hungry (), and sleeping () are methods of the dog class.
C + + class definition
Defining a class is essentially a blueprint for defining a data type. This does not actually define any data, but it defines what the name of the class means, that is, it defines what the object of the class includes and what operations can be performed on that object.
The class definition begins with the keyword class, followed by the name of the class. The body of the class is contained in a pair of curly braces. A class definition must follow a semicolon or a declaration list. For example, we use the keyword class to define the Box data type, as follows:
Class Box
{public
:
double length; Length of a box
double breadth; Breadth of a box
double height; Height of a Box
};
The keyword public determines the access property of a class member. Within a class object scope, public members are accessible outside the class. You can also specify that the members of the class are private or protected, which we will explain later.
Because Python is a dynamic language, instances created from classes can bind properties arbitrarily.
The way to bind a property to an instance is through an instance variable, or through the self variable:
Class Student (object):
def __init__ (self, name):
self.name = name
s = Student (' Bob ')
S.score = 90
However, if the student class itself needs to bind a property. You can define a property directly in class, which is a class attribute, owned by the Student class:
Class Student (object):
name = ' Student '
When we define a class attribute, this property is categorized as all, but all instances of the class can be accessed. To test:
>>> class Student (object):
... name = ' Student '
...
>>> s = Student () # Create instance S
>>> print (s.name) # Print the Name property, because the instance does not have the Name property, so it continues to find the Name property
of Class Student
>>> Print (student.name) # Print the Name property of the class
Student
>>> s.name = ' Michael ' # Bind the Name property
>>> Print (s.name) # to the instance because the instance property precedence is higher than the class property, it masks out the class's Name property,
Michael
>>> Print ( Student.name) # But the class attributes are not gone, student.name still have access to
Student
>>> del s.name # If the Name property of the instance is deleted
> >> print (s.name) # calls S.name again, because the name property of the instance is not found, the Name property of the class is displayed
Student
As you can see from the example above, you should never use the same name for instance properties and class attributes when writing a program, because instance properties of the same name will block out class attributes, but when you delete an instance attribute and then use the same name, the class attribute is accessed.