Object oriented java learning

0x00 Preface

I talked about some basic syntax of Java. Here is the core idea of Java, object-oriented.

0x01 object oriented concept

A description carried from a certain degree.

Then let's talk about the difference between object-oriented and process-oriented.

Process oriented:

特性:模块化   流程化

优点:性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源;

比如单片机、嵌入式开发、Linux/Unix等一般采用面向过程开 发,性能是最重要的因素。

缺点:没有面向对象易维护、易复用、易扩展

object-oriented:

概念
面向对象是按人们认识客观世界的系统思维方式,采用基于对象(实体)的概念建立模型,模拟客观世界分析、设计、实现软件的办法。通过面向对象的理念使计算机软件系统能与现实世界中的系统一一对应。
特性:抽象 封装 继承 多态

优点:易维护、易复用、易扩展,由于面向对象有封装、继承、多态性的特性,可以设计出低耦合的系统,使系统更加灵活、更加易于维护

缺点:性能比面向过程低

The characteristics of object-oriented programming: encapsulation, polymorphism and inheritance.

0x02 writing of class

Before writing a class, you should first understand a few concepts.

The relationship between class and object is closely linked. Class is the description of a class of things, which is abstract. An object is an instance of a class of things and is concrete. Class is the template of object, and object is the entity of class. It's like a design drawing and a finished design.

Next, let's look at the definition format of the class

public class ClassName {
//成员变量
//成员方法
}

public class Student {
    int age = 18;
    String name = "小明";
    
    public void eat(){
        System.out.println("name:"+name+"age:"+age+"eat");
        
    }
    
}

This defines a class, but if you want to use it, you need to instantiate it.

Create a new file and instantiate it in the main method.

public static void main(String[] args) {
        Student xiaoming = new Student();
        System.out.println("age:"+xiaoming.age);
        System.out.println("name:"+xiaoming.name);
        xiaoming.eat();
    }

0x03 package

Package overview:

Hide the property. If you access a property, provide public method access to it.

Step: modify the member variable with the private keyword.

Provide corresponding get and set methods for the member variables to be accessed.

This can effectively enhance the security of the code and indirectly access and modify the data.

The role of private

Private format:

public class Student {
private String name;
private int age;
public void setName(String n) {
name = n;
} 
public String getName() {
return name;
} 
public void setAge(int a) {
age = a;
} 
public int getAge() {
return age;
}
}

0x04 end

The object-oriented foundation has written these at present, but these are only a small part of them. We will continue to update Java polymorphism and inheritance later.

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
分享
二维码
< <上一篇
下一篇>>