Class and object jobs

< p align = "center" > class and object jobs

1、 Fill in the blanks

2、 Multiple choice questions

3、 Judgment question

:

It is to analyze the steps needed to solve the problem, and then use functions to implement these steps step by step. When using, you can call them one by one.

:

The purpose of establishing objects is not to complete a step, but to describe the behavior of something in the whole problem-solving step.

Class is the abstraction of object, and object is the instantiation of class

Features: no return value, the name is the same as the class name, function: initialization variable

I. this keyword has three main applications: (1) this calls the attributes in this class, that is, the member variables in the class; (2) this calls other methods in this class; (3) this calls other construction methods in this class, which should be placed in the first line of the construction method.

This is used to access its own properties and methods in a class through this.

Tip: static can modify variables, methods, code blocks and internal classes. (5 points)

Static is used in the Java language in four ways: (variable, method, code block and internal class) 1. Static variables can be classified into two types according to whether they are static or not: one is the variable modified by static, which is called static variable or class variable; The other is variables that are not modified by static, called instance variables. The difference between the two is: for static variables, there is only one copy in memory (memory saving), memory is allocated only once for static variables, and memory allocation of static variables is completed in the process of loading classes. It can be accessed directly by class name (convenient), of course, it can also be accessed through objects (but this is not recommended). For instance variables, if no instance is created, memory will be allocated for instance variables once. Instance variables can have multiple copies in memory without affecting each other (flexible). 2. Static methods. Static methods can be called directly through the class name, and any instance can also be called. Therefore, this and super keywords cannot be used in static methods, and the instance variables and instance methods of the class (that is, member variables and member methods without static) cannot be accessed directly, Only static member variables and member methods of the class to which they belong can be accessed. Because instance members are associated with specific objects! This need to understand, want to understand the truth, not memory!!! Because static methods are independent of any instance, static methods must be implemented, not abstract. 3. Static code block static code block is also called static code block. It is a static statement block independent of class members in a class. There can be multiple static statement blocks. It can be placed anywhere. It is not in any method body. These static code blocks will be executed when loading a class. If there are multiple static code blocks, The JVM will execute them in the order they appear in the class, and each code block will be executed only once.

5、 Coding question

  2. Write a java program to display the person's name and age. Define a human person. There should be two private properties in this class: name and age. Define the constructor to initialize the data member. Then define the display () method to print the name and age. Create an instance of the human in the main method and display the information.

  3. Define a circle class, which provides an attribute inside the class: radius (R), and two methods: calculate area (getarea()) and calculate perimeter (getperimeter()). The circumference and area of the circle are calculated by two methods, and the calculation results are output. Finally, define a test class to use the circle class.

  4. Construction method and overload: define a network user class. The information to be processed includes user ID, user password and email address. When creating an instance of a class, input the above three information as parameters of the constructor. When the user ID and user password are required, the default email address is the user ID plus the string "@ gameschool. Com".

6、 Topic selection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>