一、封装
//类 private:私有
public class Student {
//属性私有
private String name;//姓名
private int id;//学号
private String sex;//性别
private int age;
//提供一些可以操作这个属性的方法!
public String getName() {//按住快捷键alt+insert选择Getter and Setter
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age > 120 || age <0){//不合法
this.age = 3;
}else{
this.age = age;
}
}
public void setSex(String sex) {
this.sex = sex;
}
}
import com.oop.Demo04.Student;
/* 封装:
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统可维护增加了
*/
public class Application {
public static void main(String[] args) {
Student student = new Student();
Student s1 = new Student();
//方法名,参数列表
s1.setName("李四");
System.out.println(s1.getName());
s1.setId(2020000000);
System.out.println(s1.getId());
s1.setSex("男");
System.out.println(s1.getSex());
s1.setAge(23);
System.out.println(s1.getAge());
}
}
/*
李四
2020000000
男
23
*/
二、继承
object类
import com.oop.Demo05.Person;
/*
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统可维护增加了
*/
public class Application {
public static void main(String[] args) {
Person person = new Person();
person.getClass();
}
}
//在Java中,所有的类,都默认直接或间接继承object类
//Person 人 :父类
public class Person /*extends object*/{
//public
//protected
//default:默认的
//private
}
//学生 is 人 : 派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person{
// Person person;//组合
//ctrl+H 打开类继承关系
}
//教师 is 人: 派生类,子类
public class Teacher extends Person{
}
super
public class Application {
public static void main(String[] args) {
Student student = new Student();
//student.test("王五");
//student.test1();
}
}
public class Person /*extends object*/{
public Person(){//正常调用一个对象先走无参构造器
System.out.println("Person无参执行了");
}
protected String name = "lisi";
//私有的东西,无法被继承
public void print(){
System.out.println("person");
}
}
public class Student extends Person{
//隐藏代码:调用了父类的无参构造
public Student(){//正常调用一个对象先走无参构造器
super();//调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student无参执行了");
}
public void print(){
System.out.println("Student");
}
private String name = "wangwu";
public void test(String name){
System.out.println(name);//输出当前String name 传进的参数
System.out.println(this.name);//指代私有字符串当前的name
System.out.println(super.name);//访问父类的name
}
public void test1(){
print();//Student
this.print();//Student
super.print();//Person
}
}
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现在子类的方法或者构造方法中!
3.super和this不能同时调用构造方法!
对比 this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提:
this:没有继承也可以使用
super:只能在继承条件才可以使用
构造方法:
this();本类的构造
super();父类的构造
方法重写
import com.oop.Demo05.A;
import com.oop.Demo05.B;
public class Application {
//静态方法和非静态的方法区别很大
//静态方法 //方法的调用只和左边,定义的数据类型有关
//非静态:重写
public static void main(String[] args) {
A a = new A();
a.test();
//父类的引用指向了子类
B b = new A();//子类重写了父类的方法
b.test();//B
}
}
//继承
public class A extends B{
//Override 重写
@Override//注解:有功能的注释!
public void test() {
System.out.println("A=>test");
}
}
//重写都是方法的重写,和属性无关
public class B {
public void test(){
System.out.println("B=>test");
}
}
重写:需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数列表列表必须相同
3.修饰符:范围可以扩大但不能缩小:public>Protected>Default>private
4.抛出的异常:范围,可以被缩小,但不能扩大; ClassNotFoundException --> Exception(大)
重写,子类的方法和父类必须要一致;方法体不同!
为什么需要重写:
1.父类的功能,子类不一定需要,或者不一定满足!
Alt+Insert: override;
三、多态
import com.oop.Demo06.Person;
import com.oop.Demo06.Student;
public class Application {
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) s2).eat();//子类重写了父类的方法,执行子类的方法
s1.eat();
}
}
public class Person {
public void run(){
System.out.println("run");
}
}
/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常! ClassCastException!
3.存在的条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
1.static 方法,属于类,它不属于实例
2.final 常量;
3.private 方法;
*/
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat() {
System.out.println("eat");
}
}
/*
eat
eat
*/
1、instanceof(类型转换)
引用类型,判断一个对象是什么类型~
//import java.util.Scanner;
import com.oop.Demo06.Person;
import com.oop.Demo06.Student;
import com.oop.Demo06.Teacher;
public class Application {
public static void main(String[] args) {
//Object > String
//Object > Person > Teacher
//Object > Person > Student
Object object = new Student();
//System.out.println(X instanceof Y);//能不能编译通过!x 跟 y之间存在父子关系,编译通过;否则不通过!
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);//编译报错! Person 和 String是同级,没有上下级关系
System.out.println("---------------------------------");
Student student = new Student();
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);//编译报错! 没有上下级的关系
}
}
/*
true
true
true
false
false
---------------------------------
true
true
true
false
---------------------------------
true
true
true
*/
import com.oop.Demo06.Person;
import com.oop.Demo06.Student;
public class Application {
public static void main(String[] args) {
//类型之间的转化:基本类型转换 高低 64 32 16 8 ;高-->低 需要强转 低-->高 不需要
// 父类(高) 子类(低)
//高 低
Person obj = new Student();
//student将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
// ((Student) obj).go();
Student student = (Student) obj;//强制转换
student.go();
}
}
/*
go
*/
抽象:编程思想!
import com.oop.Demo06.Person;
import com.oop.Demo06.Student;
public class Application {
public static void main(String[] args) {
//类型之间的转换:父 子
//子类转换为父类,可能丢失自己的本来的一些方法!
Person student = new Student();
((Student) student).go();
Person person = student;
}
}
/*
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型;(丢失子类中原本可直接调用的特有的方法)
3.把父类转换为子类,向下转型,强制转换(会丢失父类被子类所重写掉的方法)
4.方便方法的调用,减少重复的代码!简洁
抽象:封装、继承、多态! 抽象类:接口
*/
------------------------------------------------------------
public class Person {
public void run(){//父类的run方法
System.out.println("run");
}
}
public class Student extends Person{
public void go(){//子类的go方法 并且继承了父类的run方法
System.out.println("go");
}
}
2、static关键字详解
//static
public class Student {
private static int age;//静态的变量 多线程!
private double score;//非静态的变量
public void run(){//非静态方法可以调用静态方法里面的东西
go();
}
public static void go(){
}
public static void main(String[] args) {//main方法也是一个static方法
new Student().run();//对象.方法
go();//在当前这个类里面
//静态方法可以调用静态方法里面的东西,而不可以调用非静态方法里面的东西
}
}
------------------------------------------------------------
public class Person {
{
//代码块(匿名代码块)---程序在执行时,并不能主动调用这个代码块
} //创建对象的时候,自动创建了,在构造器之前
static{
//静态代码块(匿名代码块)---加载一些初始化的数据
//类一加载就直接执行,永久只执行一次
}
}
------------------------------------------------------------
public class Person {//对象一创建先走匿名代码块->构造方法 如果有静态方法只会在第一次执行,后面不会执行了
//2. 赋初值
{
System.out.println("匿名代码块");
}
//1. static只执行一次
static{
System.out.println("静态代码块");
}
//3.
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("\t");
Person person2 = new Person();
}
}
/*
静态代码块
匿名代码块
构造方法
匿名代码块
构造方法
*/
//静态导入包~
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(Math.random());
System.out.println(PI);
}
}
//在父类(Person)里面加入修饰符final,则子类(Student)不能继承父类
3、抽象类
//abstract 抽象类:类 extends: 单继承~ (接口可以多继承)
public abstract class Action {
//约束~有人帮我们实现~
//abstract 抽象方法,只有方法的名字,没有方法的实现
public abstract void doSomething();
//1.不能new这个抽象类,只能靠子类去实现它;约束!
//2.抽象类中可以写普通的方法~
//3.抽象方法必须在抽象类中~
//抽象的抽象:约束~
//思考题? new,存在构造器吗?
//存在的意义 抽象出来~ 提高开发效率
}
4、接口
接口的定义与实现:
//抽象思维~Java
//定义的关键字 interface,接口都需要有实现类
public interface UserService {
//常量~public static final
int AGE = 99;
//接口中的所有定义的方法其实都是抽象的 public abstract
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
----------------------------------------------------------------
public interface TimeService {
void timer();
}
----------------------------------------------------------------
//抽象类:extends
//类 可以实现接口 implements 接口
//实现了接口的类,就需要重写接口中的方法
//多继承~利用接口实现多继承~
public class UserServiceImpl implements UserService,TimeService{
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void query(String name) {
}
@Override
public void timer() {
}
}
/*作用:
1.约束
2.定义一些方法,让不同的人实现~ 10--> 1
3.(方法都是)public abstract
4.(常量) public static final
5.接口不能被实例化~,接口中没有构造方法~
6.implements可以实现多个接口
7.必须要重写接口中的方法
*/
5、N种内部类
import com.oop.Demo10.Outer;
public class Application {
public static void main(String[] args) {
//new
Outer outer = new Outer();
//通过这个外部类来实例化内部类~
Outer.Inner inner = outer.new Inner();
inner.getID();
}
}
----------------------------------------------------------------
public class Outer {//外面的类
private int id = 1111;
public void out(){
System.out.println("这是外部类的方法");
}
public class Inner{//里面的类
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性~
public void getID(){
System.out.println(id);
}
}
}
/*
1111
*/
----------------------------------------------------------------
public class Outer {//外面的类
}
//一个Java类中可以有多个class类,但是只能有一个public class
class A{
public static void main(String[] args) {
}
}
----------------------------------------------------------------
public class Outer {//外面的类
//局部内部类
public void method(){
class Inner{
public void in(){
}
}
}
}
public class Test {
public static void main(String[] args) {
//没有名字初始化类,不用将实例保存到变量中~
new Apple().eat();//匿名对象的使用
//Apple apple = new Apple();
UserService userService = new UserService() {
@Override
public void hello() {
}
};
}
}
class Apple{
public void eat(){
System.out.println("1");
}
}
interface UserService{
void hello();
}