封装
-
该露的露,该藏的藏
- 高内聚,低耦合
-
封装(数据的隐藏)
- 通常,应禁止直接访问一个对象中数据的实际表现,而应通过操作接口来访问,这称为信息隐藏
-
属性私有:get/set
public class Application { /* 1.提高程序的安全性,保护数据 2.隐藏代码的实现细节 3.统一接口 4.给这个数据设置值 */ public static void main(String[] args) { Student student=new Student(); student.setName("ljn"); System.out.println(student.getName()); student.setAge(56); System.out.println(student.getAge()); } }
public class Student { //名字,学号,性别 private String name; private int id; private char sex; private int age; public int getAge() { return age; } public void setAge(int age) { if (age > 120 || age < 0) { this.age=3; }else { this.age = age; } } //学习()睡觉() //提供一些public的get、set方法 public String getName(){ return this.name; } public void setName(String name){ this.name=name; } //alt+insert public int getId() { return id; } public void setId(int id) { this.id = id; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } }
继承
-
继承的本质是对某一批类的抽象,从而实现对显示世界更好的建模
-
extands的意思是“扩展”。子类是父类的继承
-
java类中只有但继承,没有多继承
-
继承是类和类之间的一种关系,除此之外,类和类之间的关系还有依赖、组合、聚合扽
-
继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extands来表示
-
子类和父类之间,从意义上讲应该具有“is a”的关系
-
object类
-
super:
-
super调用父类的构造器,必须要在子类构造器的第一行
-
super只能出现在子类的方法或者构造方法
-
super和this不能同时不是调用构造方法
public class Person { public Person() { System.out.println("Person无参执行了"); } protected String name="ljn"; public void print(){ System.out.println("Person"); } }
public class Student extends Person { public Student() { //隐藏代码:调用了父类的无参构造 super();//调用父类的构造器,必须要在子类构造器的第一行 System.out.println("Student无参执行了"); } private String name="ljb"; public void test1(String name){ print();//Student this.print();//Student super.print();//Person } public void test(String name){ System.out.println(name); System.out.println(this.name); System.out.println(super.name); } public void print(){ System.out.println("Student"); } }
public class Application { public static void main(String[] args) { Student student = new Student(); } }
-
-
方法重写:需要有继承关系,子类重写父类的方法
- 方法名必须相同
- 参数列表必须相同
- 修饰符:范围可以扩大但不能缩小
- 抛出的异常:范围可以被缩小但不能扩大:ClassNotFoundException–>Exception
- alt+insert:override
public class Application { //静态方法和非静态的方法区别很大 //非静态:重写 public static void main(String[] args) { //方法的调用只和左边,定义的数据类型有关 A a=new A(); a.test();//A //父类的引用指向了子类 B b=new A();//子类重写了父类的方法 b.test();//B } }
public class A extends B{ @Override public void test() { System.out.println("A=>test()"); } }
//重写都是方法的重写,和属性无关 public class B { public void test(){ System.out.println("B=>test()"); } }
多态
-
即同一方法可以根据发送对象的不同而采用多种不同的行为方式
-
一个对象的实际类型是确定的,但可以指向对象的引用类型有很多
-
多态存在的条件
- 有继承关系
- 子类重写父类的方法
- 父类引用指向子类对象
-
注意:多态是方法的多态,属性没有多态性
public class Application { public static void main(String[] args) { //一个对象的实际类型是确定的 //可以指向的引用类型就不确定了 Student student1 = new Student(); Person student2 = new Student(); Object student3 = new Student(); student2.run();//子类重写了父类的方法,执行子类的方法 student1.run(); //对象能执行哪些方法,主要看对象左边的类型,和右边的关系不大 student1.eat(); //student2.eat(); } }
public class Student extends Person{ @Override public void run() { System.out.println("son"); } public void eat(){ System.out.println("eat"); } }
public class Person { public void run(){ System.out.println("run"); } }
public class Application {
public static void main(String[] args) {
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher); //false
System.out.println(object instanceof String);//false
System.out.println("-------------------------------------------");
Person person = new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher); //false
//System.out.println(person instanceof String);编译就报错
Student student=new Student();
System.out.println("-------------------------------------------");
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teacher); 编译就报错
//System.out.println(student instanceof String);编译就报错
}
}
public class Application {
public static void main(String[] args) {
//类型之间的转换:父 (高) 子(低)
Person obj = new Student();
//student将这个对象转换为Student类型,我们就可以使用Student类型的方法了
((Student) obj).go();
Student student = new Student();
student.go();
//子类转换为父类,可能丢失自己本来的一些方法
Person person=student;
}
}
static关键字详解
//static
public class Student {
private static int age;//静态的变量 多线程!
private double score;//非静态的变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
new Student().run();
Student.go();
go();
/*Student student=new Student();
System.out.println(Student.age);
//System.out.println(Student.score);不行
System.out.println(student.score);
System.out.println(student.age);*/
}
}
{
//代码块(匿名代码块)
}
static {
//静态代码块
}
public class Person {
//2:赋初始值
{
System.out.println("匿名代码块");
}
//1:只执行一次
static {
System.out.println("静态代码块");
}
//3
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("---------------------------------");
Person person2 = new Person();
}
}
//静态导入包~
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test1 {
public static void main(String[] args) {
System.out.println(Math.random());
System.out.println(PI);
}
}