封装:解决数据(对象的成员变量的值)安全问题
继承:继承的使用解决 代码冗余(重复)问题
多态:多态的使用 解决 程序扩展问题(功能新增)
封装
-
使用封装 解决数据(对象的成员变量的值)安全问题
-
封装的使用步骤
-
所有的成员变量都使用private 修饰
-
给成员变量 提供公有的 get/set方法
-
//封装的使用 public class Car { //权限修饰符 public:公共的 共有的;private:私有的私密的 不能出本类体 本类中可以任意访问 private String brand; private double price; public Car() { } public Car(String brand, double price) { this.brand = brand; this.price = price; } public String getBrand() { return this.brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return this.price; } public void setPrice(double price) { //有效性 校验:保证数据的有效性 if (price >= 15 && price <= 19) { this.price = price; } } public void run() { System.out.println(price); } }
测试类
public static void main(String[] args) { //通用的类型 提前定义好 /*String str; Scanner sc = new Scanner();*/ Car car1 = new Car("比亚迪", 19); method1(car1); System.out.println(car1.getPrice()); } public static void method1(Car car) { String brand = car.getBrand(); double price = car.getPrice(); car.setPrice(16); // car.price = -10; }
关键字static
-
static关键字 表示静态的;static修饰类,成员变量 ,方法,代码块
-
static修饰成员变量 表示是个静态的成员变量
-
静态的成员变量;属于整个类的不再属于某个对象;值只存储一份存储在方法区中 此类型共享这个数据;
-
当类型被加载的时候已经赋值了 JVM给的零值 类型被加载的时机远远地早于构造方法的执行(也就是说早于对象的创建);
-
访问方式上 使用 类型名.静态成员变量名
-
1,修饰成员变量
public class Student { //static内部类 /*private static class T { }*/ //普通成员变量的值存储在对象中堆,一定要有对象才会有普通成员变量的值,每个对象各自存储一份 private int id; private String name; //静态的成员变量;属于整个类的不再属于某个对象;值只存储一份存储在方法区中 此类型共享这个数据 //当类型被加载的时候已经赋值了 JVM给的零值 类型被加载的时机远远地早于构造方法的执行(也就是说早于对象的创建); public static int count; //静态代码块 static { } public Student() { count++; } public Student(int id, String name) { count++; this.id = id; this.name = name; } /** * 静态方法 */ public static void study() { } }
测试代码
/** * static修饰成员变量 */ private static void method1() { // 推荐使用 类型名.静态成员变量名 System.out.println(Student.count); System.out.println(Math.PI); Student student1 = new Student(); Student student3 = new Student(); // System.out.println(student1.count); Student student2 = new Student(10, "tom1"); System.out.println(student2.count); System.out.println(Student.count);
2,static修饰方法
public class Student2 { private int id; private String name; private static int count; public Student2() { } public Student2(int id, String name) { this.id = id; this.name = name; } /** * 静态方法:和对象没关系 属于真个类的 调用方式 类型名.方法名(); * ****不能出现非静态的成员(成员变量,方法) * ****不能出现this关键字 */ public static void read() { method1(); System.out.println(count + "在阅读"); } public static void method1() { } /** * 实例(对象 instance)方法,一定要先有对象才能调用的方法也称之为实例方法 */ public void study() { System.out.println(name + " "); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
测试代码
/** * static修饰方法 */ private static void method2() { /* Student2 student2 = new Student2(); student2.study(); //不推荐 student2.read();*/ //类型名.方法名(); Student2.read(); /**/ method1(); System.out.println(userName); //Math方法都是静态方法; 工具方法 Math.pow(2, 3); }
3,static修饰代码块
public class Student3 { private int id; private String name; private static int count = 10; //静态代码只执行一次 在类加载时刻执行的 (因为类型只会被加载一次) //资源提前加载动作 放到静态代码块中例如 数据库连接池资源 线程池 static { System.out.println("静态代码块执行了"); } public Student3() { System.out.println("无参构造执行了"); } public Student3(int id, String name) { this.id = id; this.name = name; } }
测试代码
private static void method3() { Student3 student3 = new Student3(); Student3 student32 = new Student3(); Student3 student33 = new Student3(); }