面向对象
面向对象的本质就是:以类的方式组织代码,以对象的形式组织(封装)数据。
三大特性:
- 封装
- 继承
- 多态
从认识论的角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对对象的抽象。
从代码运行的角度考虑是先有类后有对象。类是对象的模板。
类与对象的关系
类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但是并不能代表某一个具体的事物,
动物、植物、手机、电脑···
Person类、Pet类、Car类等,这些类都是用来描述/定义某一类具体的事物应该具备的特点和行为
对象是抽象概念的具体实例
张三是人的一个具体实例,张三家的旺财就是狗的一个具体实例。
能够体现出特点,展现出功能的是具体的实例,而不是一个抽象的概念。
创建与初始化对象
使用new关键字创建对象
使用new关键字创建的时候,除了分配内存空间之外,还会给创建好的对象 进行默认的初始化 ,以及对类中构造器的调用。
类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的。并且构造器有以下俩个特点:
1、必须和类的名字相同
2、必须没有返回类型,也不能写void
类
类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但是并不能代表某一个具体的事物,
动物、植物、手机、电脑···
Person类、Pet类、Car类等,这些类都是用来描述/定义某一类具体的事物应该具备的特点和行为
对象是抽象概念的具体实例
张三是人的一个具体实例,张三家的旺财就是狗的一个具体实例。
能够体现出特点,展现出功能的是具体的实例,而不是一个抽象的概念。
//一个项目应该只存在一个main方法
public class Application {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
/*Person person = new Person("leshen");
System.out.println(person.name);*/
}
}
构造器
1、和类名相同
2、没有返回值
作用:
1、new本质在调用构造方法
2、初始化对象的值
注意点:
定义有参构造之后,如果想要使用无参构造,显示的定义一个无参的构造
alt+insert生成构造器
public class Person {
//一个类即使什么都不写也会存在一个方法
String name;
//实例化初始值
//1、使用new关键字,必须要有构造器,本质是在调用构造器
//2、用来初始化值
//alt+insert生成构造器
public Person(){
}
public Person(String name){
this.name = name;
}
}
//学生类
public class Student {
//属性:字段
String name;
int age;
//方法
public void study(){
System.out.println(this.name+"学生在学习");
}
}
/*
测试类
//类:抽象的,实例化
//类实例化后会返回一个自己的对象
//studen对象就是Student的具体实例
Student xiaoming = new Student();
Student xiaohong = new Student();
xiaoming.name = "小明";
xiaoming.age = 3;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
xiaohong.name = "小红";
xiaohong.age = 3;
System.out.println(xiaohong.name);
System.out.println(xiaohong.age);
*/
封装
该露的露,该藏的藏。
我们程序要追求“高内聚,低耦合”。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。
封装
通常应禁止直接访问一个对象中的数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。
记住这句话:get/set
1、提高程序的安全性,保护数据
2、隐藏代码的实现细节
3、统一接口
4、增加了系统可维护性
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("秦疆");
System.out.println(s1.getName());
}
}
//类 private:私有
public class Student {
//属性私有
private String name; //名字
private int id; //学号
private char sex; //性别
//提供一些可以操作这个属性的方法!
//提供一些public的get、set方法
//get获得这个数据
public String getName(){
return this.name;
}
//set 给这个数据设置值
public void setName(String name){
this.name = name;
}
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;
}
}
继承
继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模
extends的意思是“扩展”。子类是父类的扩展
java类中只有单继承,没有多继承
继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。
继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
子类和父类之间,从意义上讲应该具有“is a”的关系
object类
super-this
方法重写
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
System.out.println(student.money);
}
}
//在Java中,所有类,都默认直接或间接继承Object
//Person 人:父类
public class Person {
//public公共的
//protected
//default
//private私有的
public int money = 10_0000_0000;
public void say(){
System.out.println("乐神");
}
}
//学生 is 人 : 派生类:子类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person{
//Ctrl + H 查看父类
}
Super与this
super注意点:
1、super调用父类的构造方法,必须在构造方法的第一个
2、super必须只能出现在子类的方法或者构造方法中!
3、super和this不能同事调用构造方法。
//学生 is 人 : 派生类:子类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person{
//Ctrl + H 查看父类
super Vs this:
代表对象不同;
this:本身调用者这个对象
super:代表父类对象的应用
前提
this:没有继承也可以使用
super:只能在继承条件才可以使用
构造方法
this()调用本类的构造
super()调用父类的构造
重写:需要有继承关系,子类重写父类的方法
1、方法名必须相同
2、参数列表,列表必须相同
3、修饰符:范围可以扩大但不能缩小 publicDefault>private
4、抛出的异常:范围,可以被缩小,但不能扩大
重写,子类的方法和父类必须一致:方法体不同!
为什么需要重写:
1、父类的功能,子类不一定需要,或者不一定满足!
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("秦疆");
student.test1();
}
}
public class Person {
protected String name = "狂神";
public Person(){
System.out.println("Person无参执行了");
}
public void print(){
System.out.println("Person");
}
}
public Student(){
//隐藏代码,调用了父类的无参构造
//super();//调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student无参执行了");
}
private String name = "qinjiang";
public void print(){
System.out.println("Student");
}
public void test1(){
print();
this.print();
super.print();
}
public void test(String name ){
System.out.println(name);//秦疆
System.out.println(this.name);//qinjiang
System.out.println(super.name);//狂神
}
}
方法重写
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 B {
public void test(){
System.out.println("B=>test()");
}
}
public class A extends B{
//Override 重写
@Override //注解:有功能的注解!
public void test() {
System.out.println("A=>test()");
}
}
多态
多态注意事项:
1、多态是方法的多态,属性没有多态
2、父类和子类,有联系 类型转换异常! ClassCastException!
3、存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
public class Application {
public static void main(String[] args) {
//Student 能调用的方法都是自己的或者父类的应用指向父类
Student s1 = new Student();
//Person父类型,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
s1.run();
s2.run();//子类重写了父类的方法,执行子类的方法
((Student) s3).run();
s1.eat();
((Student) s2).eat();
}
}
public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person {
public void run(){
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
instanceof和类型转换
instanceof和类型转换总结:
1、父类引用指向子类的对象
2、把子类转换为父类,向上转型;直接转换
3、把父类转换为子类,向下转型:强制转换
4、作用方便方法的调用,减少重复的代码
public class Application {
public static void main(String[] args) {
//类型之间的转换: 父 子
//高 低
Person s = new Student();
//student将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
((Student) s).go();
}
}
public class Person {
public void run(){
System.out.println("run");
}
}
package com.oop.demo09;
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
/*
//System.out.println(X instanceof Y);//能否编译通过
Object object = new Student();
System.out.println(object instanceof Student);
System.out.println(object instanceof Person);
System.out.println(object instanceof Object);
System.out.println(object instanceof Teacher);
System.out.println(object instanceof String);
System.out.println("=====================");
Person person = new Student();
System.out.println(person instanceof Student);
System.out.println(person instanceof Person);
System.out.println(person instanceof Object);
System.out.println(person instanceof Teacher);
//System.out.println(person instanceof String);//编译报错
System.out.println("=====================");
Student student = new Student();
System.out.println(student instanceof Student);
System.out.println(student instanceof Person);
System.out.println(student instanceof Object);
//System.out.println(student instanceof Teacher);//编译报错
//System.out.println(person instanceof String);//编译报错
System.out.println("=====================");
*/
public class Teacher extends Person{
}
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();//非静态方法调用也需要new对象
Student.go();
Student s1 = new Student();
System.out.println(Student.age);
System.out.println(s1.age);
System.out.println(s1.score);
//System.out.println(Student.score); 不能直接调用
}
}
public class Person {
//匿名代码块 赋初值
{
System.out.println("匿名代码块");
}
//静态代码块 只执行一次
static {
System.out.println("静态代码块");
}
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.PI;
import static java.lang.Math.random;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}
抽象类
// abstract 抽象类: 类 extends: 单继承 (接口可以多继承)
public abstract class Action {
//约束~有人帮我们实现
//abstract,抽象方法,只有方法名字,没有方法的实现
public abstract void doSomething();
//1、不能new这个抽象类,只能靠子类去实现它:约束的作用!
//2、抽象类中可以写普通的方法
//3、抽象方法必须在抽象类中
//抽象的抽象:约束~
}
//抽象类的所有方法,继承了它的子类,都必须要实现它的方法~ 除非
public class A extends Action{
@Override
public void doSomething() {
}
}