JavaSE笔记_day05_面向对象、封装

142 阅读7分钟

一.面向对象思想

1.面向对象概念

  • 面向过程:当需要实现一个功能的时候,每一个具体的步骤都需要亲力亲为,详细处理每一处细节。
  • 面向对象:当需要实现一个功能的时候,不关心具体的步骤,而是找一个已经具有该功能的人,来帮我做事儿。 
  • 面向对象和面向过程之间的关系? 
              面向对象基于面向过程。 
  • 面向对象的好处? 
              a.更符合人类的思维方式,有事情找别人帮你实现,自己轻松。 
              b.面向对象将复杂的问题简单化。 
              c.由实施者变成了指挥者。 
  • 面向对象三大特征:封装性、继承性、多态性

2.类和对象的关系

  • 类:就是事物的一个抽象的概念,抽象的表示方式 
      抽象:无法准确描述的,无法形容的 

              举例:玩具模型 ---> 抽象存在 

  • 对象:事物的具体的表现方式,也是类的具体的表现方式 
              举例:皮卡丘 ---> 具体存在 ---> 对象 

     类是对象的模板(抽象的玩具模型) 

     对象是类的实体(具体的皮卡丘)

3.类的组成

  • 类:属性 + 功能的集合

* 属性:特征描述:   姓名
*                  年龄
* 功能:能做什么:   吃饭
*           	   睡觉
*                  学习
* 对应到java的类中:
* 属性(又称成员变量,定义在类中方法外的位置):
*               String name;
*               int age;
* 功能(又称成员方法):普通方法带有static关键字,成员方法无static关键字
*               public void eat(){} //吃饭
*               public void sleep(){}//睡觉
*               public void study(){}//学习

4.对象的创建和使用

  • 定义一个汽车类,有颜色(color),轮胎个数(num),功能为跑:run() {   要求在实现跑的功能时,显示汽车的颜色和轮胎个数   } 测试类中,创建汽车对象,修改成员变量的值,调用run方法。

* 通常情况下,一个类并不能直接使用,需要根据类创建一个对象,才能使用。
* 手机图纸无法使用,根据图纸创造手机才能使用
* 1.导包:指出需要使用的类在什么位置。
*       import 包名称.类名称
*       import cn.itcast.bjzhang.demo01.Student;
*       对于和当前类属于同一个包的情况下,可以省略导包语句不写。
* 2.创建
*        类名 对象名 = new 类名(); //Random r = new Random(); 类是一种引用数据类型
*        Student stu = new Student();
* 3.使用
*       如何使用成员变量?对象名.成员变量名
*       如何使用成员方法?对象名.成员方法名(参数)
===================================================
public class TestCar {
    public static void main(String[] args) {
        //2.创建对象
        Car c = new Car();
        //3.使用
        c.num = 4;
        c.color = "red";
        c.run();
        }
    }
    public class Car {//定义一个汽车类
        String color; //汽车属性,成员变量 
        int num;/*默认初值*/
        public void run(){//汽车功能,成员方法
            System.out.println("一辆"+num+"个轱辘的"+color+"汽车在跑");
        }
}

5.类在内存中的运行过程

  • a.方法区:加载类的.class文件(TestCar.class、Car.class) 
  • b.--> 栈内存:main() 自上至下执行 - Car c = 
  • c.--> 堆内存:new Car(); - String color; int num; //默认值 
  • d.--> 栈内存:main() - 赋值语句 - run(){打印}

6.成员变量和局部变量的区别

* a.定义位置不同
*       成员变量:在方法外部,直接写到类当中
*       局部变量: 在方法内部
* b.内存位置不同
*       局部变量位于栈内存
*       成员变量位于堆内存 new
* c.作用
*       成员变量:整个类都可以通用
*       局部变量:只有方法当中才能使用,出了方法不能使用
* d.默认值
*       成员变量:有默认值
*       局部变量:一定需要先赋值
* e.生命周期
*       局部变量随着方法进栈而诞生,方法运行完毕,随着方法出栈而消失(通常短)
*       成员变量随着对象创建而诞生,不再使用时,随着对象被垃圾回收而消失(通常长)

7.匿名对象

  • 匿名对象就是只有右边的对象,没有左边的名字和赋值运算符 
  • new 类名称; 
  • 注意事项:匿名对象只能使用唯一的一次,下次使用不得不再创建一个新对象 
  • 使用建议: 如果针对类中的方法或者属性,只需要使用一次,就可以使用匿名对象来完成。 
  • 节省空间、只能用一次

public class Demo01Anonymous {
    public static void main(String[] args) {
        Person one = new Person();//左边的one就是对象的名字
        one.name = "高圆圆";
        one.show();//我叫高圆圆
        System.out.println("+++++++++++++++++++++++++++");
	//匿名对象
	new Person().name = "赵又廷";
	new Person().show();//我叫: null
    }
}
public class Person{
    String name;//成员变量
    public void show(){//成员方法
    System.out.println("我叫:"+name);
    }
}

  • 匿名对象作为方法的参数

public class Demo02Anonymous {
    public static void main(String[] args) {
        //普通使用方式
        Scanner sc = new Scanner(System.in);
        int num1 = sc.nextInt();
	System.out.println("输入的数字是: "+num1);

	//匿名对象方式
	int num2 = new Scanner(System.in).nextInt();
	System.out.println("输入的是: "+num2);

	//使用一般写法传入参数
	Scanner sc = new Scanner(System.in);
	methodParam(sc);

	//使用匿名对象进行传参
	methodParam(new Scanner(System.in));
	
	//匿名对象作为返回值
	Scanner sc = methodReturn();
	int num = sc.nextInt();
	System.out.println("输入的是: "+num);
	}

	//匿名对象进行传参
	public static void methodParam(Scanner sc){
	    int num = sc.nextInt();
	    System.out.println("最后输入的是:"+ num);
	}
				
        //匿名对象作为返回值
	public static Scanner methodReturn(){
	    return new Scanner(System.in);
        }
}

8.基本数据类型作为参数传递 (传值 - 形参的变化不会影响实参)

public class ParamJiBen {
    public static void main(String[] args) {
        int x = 5;
        getNUmber(x);
        System.out.println(x);//5
    }
    public static void getNUmber(int a ) {
        a = 10;
        System.out.println(a);//10
    }
}

9.引用数据类型作为参数传递 (传址 - 形参实参共用一个地址)

public class ParamYinYong {
    public static void main(String[] args) {
        Person m = new Person();
        System.out.println(m.name);///张三
        getPerson(m);
        System.out.println(m.name);//李四
    }
    public static void getPerson(Person p){
        p.name = "李四";
        System.out.println(p.name);//李四
    }
}
public class Person {//
    String name = "张三";//成员变量
    int age = 20;
    public void work(){//成员方法
        System.out.println("工作!");
    }
    public void eat(){
        System.out.println("吃饭");
    }
}

二.封装性 (提高代码的安全性、提高代码的复用性)

  •   问题描述:

                定义person的年龄时,无法阻止不合理的数值被设置进来

1.private关键字 (面向对象中封装特性的一种表现形式)

  • 修饰: 成员变量、方法、构造方法 
  • 一旦进行private进行修饰,只能在本类当中访问。出了本类范围不能直接访问

2.Getter and Setter

  • 对于Getter来说,不能有参数,只能有返回值 
  • 对于Setter来说,必须有参数,不能有返回值 
  • 间接访问private成员变量,就是定义一对Getter/Setter方法

public class Test {
    public static void main(String[] args) {
        Person p = new Person();
        /*System.out.println(p.age);报错,私有成员方法*/
        /*p.age = 22;报错,私有成员方法*/
        p.setAge(22);
        System.out.println(p.getAge());//22
     }
}
public class person {
    String name;
    private int age;
    public void show(){
        System.out.println("我叫:"+name+" 年龄:"+age);
    }
    public void setAge(int num){//这个成员方法专门向age设置数据
        if (num<100 && num>=0)
            age = num;
        else
            System.out.println("数据不合理");
    }
    public int getAge(){//这个成员方法专门获取age的数据
        return age;
    }
}

3.变量的就近访问原则 (若局部变量与成员变量同名,局部变量优先)

public class BianliangVisit {
    int i = 10;//成员变量
    public void getI(){
    /*  int i = 99;//局部变量
        System.out.println(i);//99,变量的就近访问原则*/
        System.out.println(i);//10
    }
    public static void main(String[] args) {
        BianliangVisit b = new BianliangVisit();
        b.getI();//99、10
    }
}

4.this关键字 (表示本类对象的引用)

  • 当方法的局部变量和类的成员变量重名的时候,会优先使用局部变量 
  • 如果需要访问本类当中的成员变量,需要使用格式:

             this.成员变量名

  • "谁调用的方法谁就是this"     --   Person调用的sayHello Person就是this

例题1:
public class ValueVisit {
    int i = 10;//成员变量
    public void getI(){
        int i = 99;//局部变量
        System.out.println(i);//局部,99,变量的就近访问原则
        System.out.println(this.i);//成员,10,this.i相当于new ValueVisit().i
    }
public static void main(String[] args) {
    ValueVisit b = new ValueVisit();
    b.getI();//99、10
    }
}
例题2:
public class Person {
    String name;
    public void sayHello(String name){ //参数name是对方的名字
        System.out.println(name + ",你好"+" 我是 "+this.name);//成员变量this.name是我自己的名字
    }
}
例题3:猜数游戏
import java.util.Random;
import java.util.Scanner;
public class BianliangVisit {
    public static void main(String[] args) {
        Random r = new Random();
        int x = r.nextInt(100)+1;//生成随机数[1,100]
        boolean b = false;
        while (b == false){//没有猜中循环
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个数:");
            int y = sc.nextInt();
            if(x == y){
                System.out.println("恭喜你猜中了!");
                b = true;
            }else if(y > x){
                System.out.println("大了");
            }else{
                System.out.println("小了");
            }
        }
    }
}