类与对象
1、类与对象
类是一个模板:抽象,对象是一个具体的实例
2、方法
定义、调用!
3、对应的引用
引用类型:基本类型(8)
对象是通过引用来操作的:栈—>堆
4、属性:字段Field 成员变量
默认初始化:
数字: 0 , 0.0
char: u0000
boolean: false
引用:null
修饰符 属性类型 属性名 = 属性值!
5、 对象的创建和使用
- 必须使用new 关键字创造对象,构造器 Person xiaozhang = new Person();
- 对象的属性 xiaozhang.name
- 对象的方法 xiaozhang.sleep()
6、类(三大特性)
静态的属性 属性
动态的行为 方法
封装
属性私有:private
get/set
/*
1、提供程序的安全性,保护数据
2、隐藏代码的实现细节
3、统一接口
4、系统可维护增加了
*/
继承 extends
this vs super
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现在子类的方法或者构造方法中
3.super和this不能同时调用构造方法!
vs this:
代表的对象不同:
this: 本身调用者这个对象
super:代表父类对象的应用
前提
this:没有继承也可以使用
super:只能在继承条件才可以使用
构造方法
this():本类的构造
super():父类的构造!
public static void main(String[] args) {
Student student = new Student();
// student.test("小张");
// student.test1();
}
package com.oop.demo05;
/**
* @ClassName Person
* @Description TODO
* @Author Administrator
* @Date 2021/1/28 23:51
* @Version 1.0
**/
//在Java中,所有的类,都默认直接或间接继承Object类
//Person 人:父类
public class Person {
protected String name = "xiaozhang";
//私有的东西无被继承
//private void print(){
public void print(){
System.out.println("Person");
}
public Person(String name) {
this.name = name;
}
public Person() {
System.out.println("Person无参构造");
}
}
package com.oop.demo05;
/**
* @ClassName Student
* @Description TODO
* @Author Administrator
* @Date 2021/1/28 23:52
* @Version 1.0
**/
// 学生 is 人:派生类,子类
// 子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person {
private String name = "zhangtongxue";
public void print(){
System.out.println("student");
}
public Student() {
//隐藏代码;调用了父类的无参构造
super("name");//调用父类的构造器,必须要在子类构造器的第一行
System.out.println(" Student无参构造");
}
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
public void test1(){
print();//属于Student类
this.print();//属于Student类
super.print();//属于父类Person类的方法
}
}
方法重写 override
重写:需要又继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大但不能缩小 public>protected>default>private
4.抛出异常:范围,可以被缩小,但不能扩大;classNotFoundException-->Exception(大)
为什么需要重写:
1.父类的功能,子类不一定需要,或者不一定满足!
快捷键: Alt + insert ;
package com.oop;
import com.oop.demo03.Pet;
import com.oop.demo05.A;
import com.oop.demo05.B;
import com.oop.demo05.Student;
import javax.sound.midi.Soundbank;
public class Application {
public static void main(String[] args) {
//静态方法和非静态方法的区别
/*
如果调用的是静态方法,只和左边定义的数据类型有关,因为是和类同一时间被加载的
A a =new A();
a.test(); //A=>test()
B b = new A();
b.test();//B=>test()
*/
//方法的重写之和非静态方法有关,
A a =new A();
a.test(); //A=>test()
//父类的引用指向了子类
B b = new A();//子类重写了父类的方法
b.test();//A=>test()
}
}
package com.oop.demo05;
public class B {
/*
public static void test(){
System.out.println("B=>test()");
}
*/
public void test(){
System.out.println("B=>test()");
}
}
package com.oop.demo05;
public class A extends B {
/*
public static void test(){
System.out.println("A=>test()");
}
*/
public void test(){
System.out.println("A=>test()");
}
}
方法重写(override) vs 方法重载 (overload)
构造器(Constructor)不能被override(重写),但是可以被重载,所以我们可以看到一个类中有多个构造函数的情况
多态
什么是多态
package com.oop.demo06;
/**
* @ClassName Person
* @Description TODO
* @Author Administrator
* @Date 2021/1/29 22:53
* @Version 1.0
**/
public class Person {
public void run(){
System.out.println("run");
}
}
/*
public static void main(String[] args) {
//一个对象的实际类型是确定的
// new Student();
// new Person();
//可以指向的引用类型就不确定了:父类的引用指向子类对象
//Student 能调用的方法都是自己的或者是继承父类的!
Student s1 = new Student();
//Person 父亲类,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 =new Student();
//对象能执行哪些方法,追咬看对象左边的类型,和右边关系不大!
((Student) s3).eat();// s3.eat();
((Student) s2).eat();// s2.eat();
}
/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常! ClassCastException!
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
1.static 方法,属于类,它不属于实例
2.final 常量;
3.private 方法;
*/
package com.oop.demo06;
/**
* @ClassName Studet
* @Description TODO
* @Author Administrator
* @Date 2021/1/29 22:53
* @Version 1.0
**/
public class Student extends Person {
public void run(){
System.out.println("walk");
}
public void eat(){
System.out.println("eat");
}
}
instanceof 关键字
package com.oop.demo07;
/**
* @ClassName Person
* @Description TODO
* @Author Administrator
* @Date 2021/1/30 1:00
* @Version 1.0
**/
public class Person {
}
package com.oop.demo07;
/**
* @ClassName Student
* @Description TODO
* @Author Administrator
* @Date 2021/1/30 1:01
* @Version 1.0
**/
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
package com.oop.demo07;
/**
* @ClassName Teacher
* @Description TODO
* @Author Administrator
* @Date 2021/1/30 1:01
* @Version 1.0
**/
public class Teacher extends Person {
}
package com.oop;
import com.oop.demo03.Pet;
import com.oop.demo05.A;
import com.oop.demo05.B;
import com.oop.demo07.Teacher;
import com.oop.demo07.Person;
import com.oop.demo07.Student;
import sun.applet.Main;
import javax.sound.midi.Soundbank;
/**
* @ClassName Application
* @Description TODO
* @Author Administrator
* @Date 2021/1/27 16:47
* @Version 1.0
**/
/*
1、提供程序的安全性,保护数据
2、隐藏代码的实现细节
3、统一接口
4、系统可维护增加了
*/
public class Application {
public static void main(String[] args) {
/*
//注意顺序
// 子类 instance of 父类 成立
// 父类 instance of 子类 不成立
//不在同一层次的,编译不通过
//修饰符 变量名(定义引用类型变量名,存在于栈中) new 构造方法
Object o = new Student();
System.out.println(o instanceof Student);//true
System.out.println(o instanceof Teacher);// false
System.out.println(o instanceof Person);// true
System.out.println(o instanceof Object);// true
System.out.println(o instanceof String);//false
System.out.println("#########################################");
Object o1 = new Person();
System.out.println(o1 instanceof Student);//false
System.out.println(o1 instanceof Teacher);//false
System.out.println(o1 instanceof Person);// true
System.out.println(o1 instanceof Object);// true
System.out.println(o1 instanceof String);// false
System.out.println("#########################################");
Person p1 = new Person();
System.out.println(p1 instanceof Student);//false
System.out.println(p1 instanceof Teacher);//false
System.out.println(p1 instanceof Person);// true
System.out.println(p1 instanceof Object);// true
// System.out.println(p1 instanceof String);// 编译出错
System.out.println("************************************************");
Person p2 = new Student();
System.out.println(p2 instanceof Student);//true
System.out.println(p2 instanceof Teacher);//false
System.out.println(p2 instanceof Person);// true
System.out.println(p2 instanceof Object);// true
//System.out.println(p1 instanceof String);// 编译出错
*/
//类型之间的转化: 父 子
Person p1 =new Student();
// p1.go(); 父类引用指向子类的对象,父类引用不能调用子类对象方法
//子类转换为父类,可能丢失自己的本来的一些方法!
Student s1 = new Student();
s1.go();
Person p2 = s1;
//p2.go(); 编译不通过
}
/*
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型;
3.把父类转换为子类,向下转型;强制转换
4.方便方法的调用,减少重复代码!简洁
*/
}