First, what are classes and objects
* Classes are molds that determine the characteristics (attributes) and methods that an object will have
* Characteristics of the class:
* Class is the type of object
* Collection of a set of objects with the same properties and methods
* Properties of the Object
* Attributes-Various characteristics of an object
* Specific values that are useful for each property of each object
* Method of the object
* Method-Action performed by the object (what can be done)
* Class and object relationships
* Classes are abstract concepts, just templates
* object is a concrete entity that you can see and touch
Second, how to define the Java class
* 1, the importance of the class: All Java programs have classes class as the organizational unit
* 2, what is the class
* Classes are molds that determine the characteristics (attributes) and methods that an object will have
* 3, the composition of the class: Properties and Methods
* 4. Steps to define a class
* A, define class name public class name {
* b, write the properties of the class//Define Attributes section
* Type of attribute 1
* C, method of writing class//Definition method part
* Method 1
*/
//1. Define a class public classes Classandobject {//2. Properties (Member methods) What is the float screen;float cpu;float mem;//3. Method what to do void call () { System.out.println ("Telphone has the function of calling! ");} void SendMessage () {System.out.println ("screen:" +screen+ "CPU:" +cpu+ "mem:" +mem+ "Telphpne has the function of texting! ");} /** * Java objects * steps to using objects * 1, creating objects: * Class Name Object name = new class name (); * Classandobjec phone = new Classandobject (); * 2, Applicable object * Properties of the referenced object: Object name. property * Phone.screen = 5;//assigns a value of 5 */public static void Main (String args[]) to the screen property {//classandobject Phon E = new Classandobject ();p hone.call ();p hone.sendmessage ()//1. Assigning a value to an instance variable phone.screen = 5.0F;PHONE.CPU = 1.4f;phone.mem = 2.0f;//2. Method of calling Object Phone.sendmessage ();}}
Third, member variables and local variables
*
* 1, member variables
* defined in the class to describe what the object will be
* 2, local variables
* defined in the method of the class to temporarily save the data in the method
* Local variables: Defined within a method, can only be used in the current method
* Member variables: This class of methods, methods of other classes, using
Difference
* 1), different scopes
* The scope of a local variable is limited to the method that defines it
* The scope of the member variable is visible within the entire class.
* 2), the initial value is different
* Java will give the member variable an initial value
* Local variable initial values are not given
* 3), in the same method, local variables with the same name are not allowed
* In different methods, you can have local variables of the same name
* 4), when two types of variables have the same name, local variables have higher precedence (nearest principle)
int var;//member variable void Test () {int localVar = 10;//local variable System.out.println ("var:" +var); System.out.println ("LocalVar" +localvar); void Test1 () {//system.out.println ("LocalVar" +localvar); public static void Main (String args[]) {Variable var = new Variable (); var. Test ();}
Iv. Construction Methods
1. Create a new object using the new+ construction method
2. The constructor method is defined in the Java class as a method for initializing the object
The constructor method has the same name as the class name and no return value
Statement format for constructed methods
Public constructor method name ([can specify parameter]) {
Initialize Code
No return value type method name is the same as class name
}
Main:public static void Main (String args[]) {//The object can be created by means of a parameterless constructor Constructionmethod phone = new Constructionmethod ();// The object can also be created by means of an argument constructor, and the instance variable in the object is assigned an initial value constructionmethod Phone2 = new Constructionmethod (5.0f, 1.4f, 2.0f);} Non-parametric construction method: Public Constructionmethod () {System.out.println ("No parameter construction method was executed");} Constructive method: Public Constructionmethod (float newscreen,float newcpu,float newmem) {screen = Newscreen;cpu = Newcpu;mem = Newmem; System.out.println ("The constructive method of the argument was executed!") ");}
* 5. The system automatically adds an parameterless construction method when no constructor method is specified
* 6. The system does not automatically add an argument-less construction method when there is a specified construction method, whether it is a constructor with a parameter or no argument
* 7. Overloading of constructor methods: multiple methods with the same method name but different parameters, automatically selecting the appropriate method according to different parameters when calling
* 8. Construction methods not only can assign values to the properties of an object, but also ensure that a reasonable value is assigned to the object's attributes.
v. Static variables, static methods
public class Staticvariable {//static modified variables are static variables, objects of all classes are shared hobbystatic String hobby = "originally IMOOC";// Use the static keyword to declare a static method public static void print () {System.out.println ("static Method");} public static void Main (string[] args) {//TODO auto-generated method stub/** * Static variable: * A static Member or class member in Java that is modified by static. It belongs to the entire class. * Static members can be accessed directly using the class name, or can be accessed using the object name * Using static can modify variables, methods, and code blocks *///static variables can be accessed directly using the class name, without creating an object of the class System.out.println ( Staticvariable.hobby);//create Object of class staticvariable Svar = new staticvariable (); System.out.println (svar.hobby);//Modify the static variable in the form of the object name svar.hobby = "Modified Hello";//Use the class name again to access the static variable, the value has been modified SYSTEM.OUT.PRINTLN ( Svar.hobby);/** * static method: * Call static method directly using class name * Also can be called by the object name, of course, it is recommended to use the class name called *///Class name call static method Staticvariable.print ();/ The object name is called staticvariable svar_2 = new staticvariable (); Svar_2.print ();}} public class Staticset {/** * * static initialization block in Java * Data assignment by initialization block * In the declaration of a class, you can include multiple initialization blocks that, when you create an instance of the class, execute the block of code in turn: Static initialization block (using St atic modifier initialization block) * Static initialization blocks are executed only once when the class is loaded, and static initialization blocks can only assign values to static variables, and cannot initialize normal member variables * Static initialization blocks are first performed--normal initialization block-----construction method */int name1; int name2;//defines a membervariable static int name3;public Staticset () {//The member variable is assigned a value by initialization block, construction method {name1 = 91; SYSTEM.OUT.PRINTLN ("assigns a value to the variable name1 by constructing the method. ");}} Initialization block {name2 = 92; System.out.println ("Assignment of variable name2 by initialization block");} static{//static initialization block Name3 = 93; System.out.println ("Assignment of static variable Name3 by static initial block");} public static void Main (String args[]) {Staticset SSet = new Staticset (); System.out.println ("name1:" +sset.name1); System.out.println ("name2:" +sset.name2); System.out.println ("Name3:" +name3); Staticset SSet2 = new Staticset ();}
Classes and objects in "programming language" Java learn by Imooc