阅读 130

JAVA中的内存结构、重载、this与继承

JAVA中的内存结构、重载、this与继承

一.对象在JVM的内存结构

JAVA内存管理由JVM来管理。

1)堆,所有new出来的对象(包括成员变量)

2)栈,所有局部变量(包括方法的参数)

3)方法区,class字节码文件(包括方法,静态数据)


1.引用变量指向null时,会发生空指针异常

public class student {

int age;

String name;

public student(int age,String name){

this.age=age;

this.name=name;

}

public static void main(String[] args) {

//(类)引用类型 引用变量 创建对象(new出来的类中的构造方法)

student  s=  new student(18,"刘永超");

s=null;

System.out.println(s);

System.out.println(s.age);//发生异常,因为引用变量指向null

}

}


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

结果:java.lang.NullPointerException

2.引用类型划等号和基本类型划等号的区别:

一.引用类型划等号:

1…指向同一个对象


2.通过一个引用的修改对象中的数据会影响另一个对象中的数据


二.基本类型划等号:

1.赋值。


2.对一个变量的修改不会影响到另一个变量(例如int类型)


引用变量画“”和基本类型画”“区别:

引用类型画”==“:


1.判断两个引用变量(引用地址)是否指向同一对象


2.基本类型画等号:判断两边的值是否相等


代码演示:

public class student2 {

int age;

String name;

public student2(int a,String s){

age=a;

name=s;

}

public static void main(String[] args) {

student2 s=new student2(17,"liu");

student2 ss=s;

s.age=20;

ss.age=28;

System.out.println(s.age);

//s=ss  判断两个引用对象(引用变量)是否指指向同一对象

System.out.println(s==ss);

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

结果为 s.age=28 true;

成员变量与局部变量的生命周期:

成员变量:创建对象开始到被GC垃圾回收器处理掉为止。

局部变量从调用方法到方法就结束。

二.方法的重载

1).发生在同一类,方法名相同,参数列表类型和数量不同

2).方法的重载,和返回值没有关系

3).编译器在在编译时根据方法的签名自动绑定调用的方法


注:方法的签名:方法名+参数列表


代码的实现:

public class overloadDemo {

public void test(){

System.out.println("lala");

}

public void test(int a){

System.out.println("heihei");

}


public void test(String s){

System.out.println("xixi");

}


public void test(int a,String s){

System.out.println("caocao");

}


// public int test(){不是方法的重载,和返回值没有关系

// return 1;

// }

//构造方法

public overloadDemo (){

System.out.println("无参构造");

}

public overloadDemo(int a){

System.out.println("有参构造");

}

public static void main(String[] args) {

overloadDemo load=new overloadDemo();

load.test(5);

load.test(8, "liu");

}

}


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

如上代码,load.test(5)将调用public void test(int a){};


load.test(8,“liu”)将调用public void test(int a,String s){}。


三.this的使用:

1.this关键字在方法中,用于指向调用该方法的当前对象,简单的说,那个对象调用方法,this指的就是那个对象,严格来讲,在方法中需要通过this关键字指明当前对象。

2.在构造方法中,用来初始化成员变量的参数一般和成员变量取相同的名字,这样有利于代码的可读性,但次数必须通过this关键字来区别成员变量和参数(在这里不能省略this)

3.this就是指当前对象。


this的用法:

1.this.成员变量—访问当前对象成员变量

2.this.方法名—访问当前对象的方法(一般不用)

3.this()---------调用构造方法(必须写在此构造方法中的第一行)


代码演示:

public class thisDemo {

int age;

public void test(){

System.out.println(this.age);//此处可以省略this关键字

}

public void test(int age){

System.out.println("这是带有int类的test方法重载");

this.test();//此处也可以省略this,编译器会自己默认

}

public thisDemo(){

//this(20);

this(19,"liuyongcaho");//调用当前对象时int ,string类型的构造方法

this.age=18;

System.out.println("这是一个无参构造方法");

}

public thisDemo(int age){

this();

System.out.println(age);

}

public thisDemo(int age,String name){

System.out.println(age+"     "+name);

}

public static void main(String[] args) {

thisDemo pdd=new thisDemo();

pdd.test(9);

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QA8e4G5v-1615567706657)(C:\Users\Thinkpad\AppData\Roaming\Typora\typora-user-images\image-20210312160858936.png)]


可以看出,在new对象时,会自行执行它的无参构造方法,执行完后再执行test(int a){}这个方法。


四:类的继承

1.作用:代码复用,减少代码重复

2.通过extends来继承

3.超类(父类),派生类(子类)共有的属性和行为

4.派生类继承超类后,派生类具有派生类+超类的共有属性。

5.一个超累可以拥有多个派生类,一个派生类只能有一个超类。

6.继承具有传递性

7.java中规定,在派生类的构造方法中必须先构造超类的构造方法(必须在派生类的第一行),在派生类中若没有调用超类的方法,则编译器默认提供super()来调用超类的无参构造方法,若超类自己写了构造方法,在派生类中不在默认提供super();


super的含义及使用:

1.super指代当前对象的超类对象

2.super必须放在构造方法中的第一行

3.super.成员变量-----访问超类成员变量

4.super()—调用的时超类构造方法。


代码演示如下:

父类:

/*

 * 父类(超类)

 */

public class Person {

int age;

String name;

String gender;

public Person(int age,String name){

this.age=age;

this.name=name;

System.out.println(this.age+"   "+this.name);

}

public void testperson(){

System.out.println("我是父类");

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

子类:

/*

 * 子类(派生类)

 * extends

 */

public class Teacher extends Person{

String subject;

public Teacher(){

//super();这是父类的无参构造方法

super(19,"刘德华");

System.out.println(this.age+"  "+this.name);

}

//方法的重写

public void testperson(){

System.out.println("这是子类teacher的testperson方法");

}

public static void main(String[] args) {

Person y=new Teacher();//向上造型

y.testperson();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zydw2DJx-1615567706663)(C:\Users\Thinkpad\AppData\Roaming\Typora\typora-user-images\image-20210312165717602.png)]


结果分析:

子类在创建对象时会执行子类的无参构造方法,而子类的构造方法中有父类的有参构造方法再执行子类中的testperson方法。


on方法");

}


public static void main(String[] args) {

Person y=new Teacher();//向上造型

y.testperson();

}

1

2

3

4

}



##### 运行结果:


[外链图片转存中...(img-zydw2DJx-1615567706663)]


##### 结果分析:


子类在创建对象时会执行子类的无参构造方法,而子类的构造方法中有父类的有参构造方法再执行子类中的testperson方法。




#### 


————————————————

版权声明:本文为CSDN博主「521125LYC」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_47600732/article/details/114715757


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐