当前位置:网站首页>Class and object learning

Class and object learning

2022-06-26 05:50:00 Wang Xiaoya

What are objects and classes ?

Everything is object , Everything that exists objectively is the object

class : Class is a class in real life Having common attributes and behaviors Of things abstract

object : It can be seen and touched Real existence Of Entity

A class is an abstraction of an object

Object is an entity of a class

attribute : The various characteristics of objects , Every... Of every object attribute Have specific value

Behavior : Object can perform

Class characteristics :

  • Class is the data type of an object
  • Class is a collection of objects with the same properties and behaviors

For example, the concept of mobile phone is a class , Because mobile phones have common attributes : How much memory 、 The screen size 、 The price is high and low 、 brand ……

There are also common behaviors : You can take and call 、 Can send and receive SMS 、 You can listen to music ……

The definition of a class

The importance of class : yes Java The basic unit of a program

What is the class : It is a kind of Common properties and Behavior The abstraction of things , Determine the properties and behavior that the object will have .

Class composition : attribute and Behavior

  • attribute : Pass in class Member variables To embody ( Variables outside methods in a class )
  • Behavior : Pass in class Member method To embody ( Compared with the previous method, remove static Key words can be used )

Class definition steps :

Defining classes   
Write member variables of a class
Write member methods for a class

                                 publicclass Class name {                                        

                                  // Member variables

                                      Variable 1 Data type of Variable 1;      

                                      Variable 2 Data type of Variable 2;

                                         …

                                 // Member method

                                       Method 1;

                                      Method 2;

                                       …

                                       }

Class name : mobile phone (Phone)

Member variables : brand (brand)    Price (price)

Member method : Make a phone call (call)    texting (sendMessage)

package object;
/* Mobile phones :
        Class name :
        mobile phone (Phone)

        Member variables :
        brand (brand)
        Price (price)

        Member method :
        Make a phone call (call)
        texting (sendMessage)*/

// Class definition steps ;
// Defining classes 
public class Phone {
    // Write member variables of a class 
    String brand;
    int price;

//  Write member methods for a class 
    public void call(){
        System.out.println(" Make a phone call ");
    }
    public void sendMessage(){
        System.out.println(" texting ");
    }
}

Use of objects

Create objects

Format : Class name Object name = new Class name ();

Example :Phone p = new Phone();

Use object

1: Use member variables

  • Format : Object name . Variable name
  • Example :p.brand

2: Use the member method

  • Format : Object name . Method name ()
  • Example :p.call()

Case study :

package object;

public class PhoneDemo {
    public static void main(String[] args) {
     /* Create objects 
 Format : Class name   Object name  = new  Class name ();*/
     Phone p = new Phone();
     /* Use object 
1: Use member variables 
 Format : Object name . Variable name */
        System.out.println(p.brand);
        // Call variables brand, Because not assigned , At this point, the default heap memory value is returned ,String Type default value “null”
        System.out.println(p.price);
        //price ditto ,int Type default value “0”

        // If the assignment is 
        p.brand = " Huawei ";
        p.price = 2999;
        // Once again, the output 
        System.out.println(p.brand);
        System.out.println(p.price);

/*2: Use the member method 
 Format : Object name . Method name ()*/
        p.call();
        p.sendMessage();
    }
}

Output results :

null
0
 Huawei 
2999
 Make a phone call 
 texting 

demand : Define a student class first , Then define a student test class , Complete the use of member variables and member methods through objects in the student test class

analysis :

Member variables : full name , Age

Member method : Study , do the homework

Ideas :

Define a student class

Class name :Student

Member variables :name,age

Member method :study(),doHomework()

Define student test classes

Class name :StudentDemo

Because I have to do a test , So there is a main method :main Method

Complete the use of member variables and member methods through objects in the student test class

Assign a value to a member variable , Output the value of the member variable

Calling a member method

package com.object_02;
// Create a student class 
public class Student {
    // Member variables 
    String name;
    int age;

    // Member method 
    public void study(){
        System.out.println(" study hard , Day day up ");
    }
    public void doHomework(){
        System.out.println(" The keyboard is broken , The monthly salary is over ten thousand ");
    }
}

package com.object_02;
// Student tests 
public class StudentDemo {
    public static void main(String[] args) {
        // Create objects 
        Student s = new Student();
        // Use object ( The default value is output )
        System.out.println(s.name+","+s.age);

        // assignment 
        s.name = " Chen Rui ";
        s.age = 44;

        // Once again, the output 
        System.out.println(s.name+","+s.age);

        // Usage method 
        s.study();
        s.doHomework();
    }
}

原网站

版权声明
本文为[Wang Xiaoya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260540483366.html