Object-Oriented Programming
Package
//封装 程序设计要求高内聚,低耦合
/*
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统的可维护性增加了
*/
public class Student {
private int age;
private String name;
private String sex;
//get set函数,提供外部的接口
//get/set函数快捷键alt+insert
public int getAge() {
return age;
}
public void setAge(int age) {//set函数可以保证数据的合法性
if (age>200||age<0){
this.age=0;
}else{
this.age = age;}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
/*
public class Application {
public static void main(String[] args) {
Student s1=new Student();
//s1.name="yang";属性被私有了,不能直接访问
s1.setName("yang");//通过set方法给私有属性赋值
System.out.println(s1.getName());//通过get得到私有属性
s1.setAge(212);//防止不合法的数据破坏系统
System.out.println(s1.getAge());//不合法的数据没有赋值成功
}
}
*/
inherit
father
//Person 父类
//在JAVA中都直接或间接继承了Object类
//一个父类可以有多个子类
//重写都是方法的重写,和属性无关
public class Person {
public String name="PERSON_NAME";
private int money=1_0000_0000;
public String car="BYD";
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Person() {
System.out.println("父类无参构造执行了");
}
public void print(){
System.out.println(this.name);
}
}
Subclass
//Student 子类
//一个子类只能有一个父类
public class Student extends Person {
public Student() {
//super();第一行默认存在一行父类无参构造器
//如果父类没有无参构造器则必须调用父类有参构造器
System.out.println("子类无参构造执行了");
}
String name="STUDENT_NAME";
public void print() {//方法重写,只能是public类型
System.out.println(this.name);
}
public void test(){
print();
this.print();//this
super.print();//引用父类的方法
}
}
The main program
//继承
/*
1.继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模
2.extends的意思是扩展。子类是对父类的扩展
3.JAVA只有单继承,没有多继承
4.继承是类和类之间的关系,除此之外,类之间的关系还有依赖,组合,聚合等。
5.继承关系的两个类,一个为子类(派生类),一个为父类(基类),子类继承父类用extends表示
6.子类和父类之间,从意义上讲应该具有“is a”的关系
*object类 super 方法重写*
*/
public class Application {
public static void main(String[] args) {
Student s1=new Student();//先执行父类构造器,再执行子类构造器
//s1.money;不能直接访问父类的私有对象
System.out.println(s1.car);
System.out.println(s1.getMoney());//通过接口访问
s1.test();
//静态方法:方法的调用只和左边定义的数据类型有关系
Student s2=new Student();
s2.print();
Person p1=new Student();//父类的引用指向了子类,子类重写了父类的方法
p1.print();
}
}
Method override super class object
Note super Point:
constructor call the parent class. 1 super
2 super then must subclass method or method of construction can only occur
constructor. 3 and this can not be called at the same time super
Note this point:
objects that represent different
this: those who call themselves the class
super: on behalf of the parent class object reference
premise:
this: there is no inheritance can also use
super: can only be used under conditions inherited
constructor:
this (); this class configuration
Super (); parent class structure
Rewrite: the need for inheritance, subclasses override the parent class
1. The method name must be the same
2. The list of arguments must be the same
3. modifiers: could be expanded: public> protected> default> Private
4. throw an exception : range can be reduced, not expanded. ClassNotFound
-> Exception (large)
Polymorphism
father
public class Person {
public void eat(){
System.out.println("父类的方法");
}
}
Subclass
public class Student extends Person {
@Override
public void eat() {
System.out.println("子类重写的方法");
}
public void run(){
System.out.println("子类独有的方法");
}
}
The main program
//多态
/*
1.多态是方法的多态
2.父类和子类,有联系 类型转换异常 ClassCastException
3.存在条件:继承关系,方法需要重写,父类指向子类对象!
这几种类型不能重写:1.static方法属于类,不是实例。2.final常量。3.private方法
*/
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//可以指向的引用类型就不确定了,父类指向子类
//student可以调用的方法都是继承的或者独有的方法
Student s1=new Student();
//父类,可以指向子类,但是不能调用子类独有的方法
Person s2=new Student();
Object s3=new Student();
s1.eat();//重写的方法
s1.run();//子类独有的方法
s2.eat();//如果方法被重写则调用被重写的方法
((Student) s2).run();//不能调用子类独有的方法,但能通过类型转换实现
}
}
instanceof
Determine the type and type conversion
//instanceof 判断是否是什么类型 返回false或者true
public class Application {
//Object>String
//Object>Person>Teacher
//Object>Person>Student √
public static void main(String[] args) {
Object test=new Student();
System.out.println(test instanceof Student);//true
System.out.println(test instanceof Person);//true
System.out.println(test instanceof Teacher);//false
System.out.println(test instanceof Object);//true
System.out.println(test instanceof String);//false
//类型转换 父类和子类之间
Person test1=new Student();//低向高自动转换,子类转换为父类可能丢失一些自己的方法
//将父类转换为子类就可以使用子类的方法
((Student) test1).run();//高向低需要强制转换
}
}
static static modifier
Static variables and static methods
//static
public class Demo01 {
private static int age;//静态的变量
private int score;
public void run(){}
public static void go(){}
public static void main(String[] args) {
System.out.println(age);//可以直接在静态方法里调用静态变量
//System.out.println(score);//需要在对象里调用非静态变量
go();//静态方法
//run();不能直接调用类里的方法
}
}
Static blocks and static introduction methods package
//静态导入包
import static java.lang.Math.random;//由于Math类是final修饰,所有不能被继承
public class Demo02 {
{//赋初始值
System.out.println("匿名代码块");
}
static{//只执行一次
System.out.println("静态代码块");
}
public Demo02() {
System.out.println("构造器");
}
public static void main(String[] args) {
Demo02 a=new Demo02();
//依次加载静态代码区,匿名代码区,构造器
System.out.println("=================");
Demo02 b=new Demo02();//再次创建对象时只加载匿名代码区和构造器
System.out.println(random());//由于静态导入包,可以直接调用Math类里的方法
}
}