0711作业

 一.面向对象

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

2:匿名对象是什么?应用场景是什么?
匿名对象是:没有名字的对象
应用场景是:1.调用方法,仅仅只调用一次的时候
          2.匿名对象可以作为实际参数传递

3:封装是什么?java中封装的体现有哪些?请举例说明。
封装是:指隐藏对象的属性和实现细节,仅对外提供公共访问方式。
java中封装的体现:函数方法,接口,类,都是封装
                private也是封装的一种体现形式。

4:this关键字是什么?this关键字的应用场景?
this关键字是:代表当前类的对象
5:如何使用一个类的成员
创建对象,成员赋值


========================================================
========================================================

二. 内存图

画图操作:

1.一个对象的内存图

2.两个对象的内存图

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


========================================================
========================================================

三. 自定义类

Student 类

class Demo3_Student {
    public static void main(String[] args) {
        print(10);//引用

        Student s=new Student();//创建对象
        print(s);//引用
    }

    public static void print(int x){
        System.out.println(x);
    }

    public static void print(Student stu){
        stu.name="李四";
        stu.age=23;
        stu.speak();

        
    }
}

class Student{
    String name;
    int age;

    public void speak(){
        System.out.println(name+".."+age);
    }
    

}

Phone 类

class Demo2_Phone {
    public static void main(String[] args) {
        Phone p=new Phone();
        p.brand="魅族";
        p.price=1999;
        System.out.println("手机是:"+p.brand+"..."+"价格是:"+p.price);

        p.call();
        p.sendMessage();
    }
}

/*    
* 属性:品牌(brand)价格(price)
* 行为:打电话(call),发信息(sendMessage)
玩游戏(playGame)*/

class Phone {
    String brand;
    int price;

    public void call(){
        System.out.println("打电话");
    }
    public void sendMessage(){
        System.out.println("发信息");
    }
}

Car 类

/*Car类   车 
             成员变量:颜色  轮胎数
             成员方法:跑
*/
class Demo4_Car {
    public static void main(String[] args) {
        Car c=new Car();
        c.color="红色";
        c.num=4;
        System.out.println("车的颜色是:"+c.color+"..."+"轮胎数是:"+c.num);

        c.run();
    }
}
/*Car类 
车 
成员变量:颜色  轮胎数
成员方法:跑
*/
class Car {
    String color;//颜色
    int num;     //数量
    
    public void run(){//  run 跑
        System.out.println("正在行驶中...");
    }     
}

猜你喜欢

转载自www.cnblogs.com/wty1994/p/9295826.html
今日推荐