Java object-oriented summary (a)

Java object-oriented summary (a)

 Java is a pure object-oriented language, object-oriented then what is it? A simple example is the story of an elephant loaded refrigerator. So now there is an elephant, we put it in the refrigerator, we only need to open the door and then put it in the family to stay, just put it to consider, without regard to how to put it, that is not consider the process, this idea is object-oriented.

Object-oriented three characteristics:

1, the package: the contents of all externally visible

2, inheritance: the inherited parent class functions continue to evolve

3, polymorphism: overloaded method itself is a manifestation of other polymorphisms

Classes and Objects:

Class is an abstract concept, is a kind of collectively of things, such as people, who is a class, then every one of us is one of an object, that is, objects belong to the class, class is a common product, It is an integrated feature, and the object is the product of a personality, an individual's characteristics. Class must pass in order to use an object, any object must define the operation in the class.

Class definition:

Definition: class attributes and methods by the composition, will be described using the class keyword, {} must be the entity that describes the classes.

format:

class class name {

Property name;

Method name () {

}

}

Properties: in fact, an individual's characteristics

Methods: that an individual's behavior, such as singing, sleep, talk, eat, play

Composition class: class is composed of properties and methods.

Definition of an object;

Format: name = new class name of the object class name ();

Objects can be generated in accordance with the above operation.

If you want to access the properties and methods, see Figure:

Access class properties

Object Name Property

Call the class method

Object Name. Method ()

 

Call the properties and methods in Person

class Person{

String name; //  representing a name

int age; //  representation of age

void tell(){

System.out.println ( " Name: " + name + " ; Age: " + Age);

}

};

public class OODemo02{

public static void main(String args[]){

Person per  = new Person() ; // 声明对象并实例化

per.name = "张三" ; // 设置per对象的name属性内容

per.age = 30 ; // 设置per对象的age属性内容

per.tell() ; // 调用类中的方法

}

};

关于堆栈的内存划分:

对象属于引用数据类型,所以也要进行内存的划分

|-不管任何情况下,只要调用了关键字new,则表示开辟新的堆内存空间,请看图解:

 

 

 在使用类的时候必须考虑到堆栈的分配的空间,如果一个类中没有使用new关键字,那么就会出现一下问题。

Exception in thread "main" java.lang.NullPointerException

        at OODemo03.main(OODemo03.java:12)

 

这个问题就是”空指向”问题,但是这个问题根源很简单,因为没有开辟内存空间,所以才导致这个问题。

那么本程序的方法放在哪里呢?放在方法区中。


发布了40 篇原创文章 · 获赞 7 · 访问量 1万+

Guess you like

Origin blog.csdn.net/sj_1993/article/details/50827316