0711 第八次作业

一、面向对象

1.局部变量和成员变量的区别
(1)在类中的位置不同:
  局部变量:在方法中定义或在方法声明中定义
  成员变量:在类中方法外定义
(2)在内存中的位置不同:
  局部变量:在栈中(属于方法,方法进栈)
  成员变量:在堆中(属于对象,对象进堆)
(3)生命周期不同
  局部变量:随着方法的调用而存在,方法弹栈后消失
  成员变量:随着对象的创建而存在,随着对象的消失而消失
(4)初始化值不同
  局部变量:没有默认的初始化值,使用前必须先定义和赋值
  成员变量:有默认的初始化值

2.什么是匿名对象?调用场景是什么?
匿名对象即是没有名字的对象;
匿名对象在调用中只能使用一次,好处是节省代码,不适合多次调用。匿名对象调用完毕后就是垃圾,可以被垃圾回收器回收。

3.封装是什么?java中封装的提现有哪些?举例说明:
封装是指隐藏对象的属性和实现细节,仅对外提供公共访问方式。
在java中,可以使用关键字private进行封装:
例:通过private关键字对类StudentInformation中的成员变量age进行封装,则在类class中需通过方法setAge对age进行修改,不能直接访问.

 1 class Student
 2 {
 3     public static void main(String[] args)
 4     {
 5         StudentInformation s1 = new StudentInformation();
 6         s1.name = "关羽";
 7         s1.setAge(36);
 8         s1.print();
 9     }
10 }
11 
12 class StudentInformation
13 {
14     String name;
15     private int age;
16     
17     public void setAge(int a)
18     {
19         if (age >= 0 && age <= 200)
20         {
21             age = a;
22         }else 
23         {
24             System.out.println("error");
25         }
26     }
27     
28     public int getAge()
29     {
30         return age;
31     }
32 
33     public void print()
34     {
35         System.out.println(name + "===" +age);
36     }
37 }

4.this关键字是什么?this关键字的应用场景:
在对象中,成员变量是隐式的,因为其不会在方法中定义,而是在类中方法外定义。
方法中的局部变量的变量名可以与成员变量相同,在方法中直接调用的话则优先就近使用局部变量
如果想要使用成员变量则需用this关键字进行区分,格式为 this.变量名

5.如何使用一个类的成员:
首先要进行创建对象;
创建对象的格式为:类名 对象名 = new 类名();其中对象名的命名应遵循lowerCamelCase。
然后就可以通过 对象名.变量名 和 对象名.方法名(...)对类中的变量和方法进行使用。

 二、内存图

一个对象的内存图

两个对象的内存图

三个引用两个对象的内存图

三、自定义类

Student类

 1 class Student
 2 {
 3     public static void main(String[] args)
 4     {
 5         StudentInformation s1 = new StudentInformation();
 6         s1.name = "关羽";
 7         s1.setAge(36);
 8         s1.print();
 9     }
10 }
11 
12 class StudentInformation
13 {
14     String name;
15     private int age;
16     
17     public void setAge(int a)
18     {
19         if (age >= 0 && age <= 200)
20         {
21             age = a;
22         }else 
23         {
24             System.out.println("error");
25         }
26     }
27     
28     public int getAge()
29     {
30         return age;
31     }
32 
33     public void print()
34     {
35         System.out.println(name + "===" +age);
36     }
37 }

Phone类

 1 class Phone
 2 {
 3     public static void main(String[] args)
 4     {
 5         PhoneInformation p1 = new PhoneInformation();
 6         p1.name = "水果手机";
 7         p1.price = 5000;
 8         p1.print();
 9 
10         PhoneInformation p2 = new PhoneInformation();
11         p2.name = "粮食手机";
12         p2.price = 2000;
13         p2.print();
14     }
15 }
16 
17 class PhoneInformation
18 {
19     String name;
20     int price;
21 
22     public void print()
23     {
24         System.out.println(name + "===" +price);
25     }
26 }

 Car类

 1 class Class01
 2 {
 3     public static void main(String[] args)
 4     {
 5         CarInformation c1 = new CarInformation();
 6         c1.color = "红色";
 7         c1.type = "法拉利";
 8         c1.power = 380;
 9         c1.print();
10 
11         CarInformation c2 = new CarInformation();
12         c2.color = "灰色";
13         c2.type = "五菱宏光";
14         c2.power = 9000;
15         c2.print();
16     }
17 }
18 
19 class CarInformation
20 {
21     String color;
22     String type;
23     int power;
24 
25     public void print()
26     {
27         System.out.println(color + type + "===" + power + "马力");
28     }
29 }

猜你喜欢

转载自www.cnblogs.com/chang4/p/9294892.html
今日推荐