1.什么是面向对象
![]()
2.类与对象的关系
![]()
![]()
package com.oop.demo02;
public class Student {
//属性 字段
String name;
int age;
//方法
public void study(){
System.out.println(this.name+"在学习");
}
}
package com.oop.demo02;
public class Application {
public static void main(String[] args) {
//类: 抽象的 实例化
//类实例化后返回一个自己的对象!
//student对象是一个Student类的具体实例
Student student = new Student();
student.name = "张三";
student.age = 18;
student.study();
}
}
3.构造器
package com.oop.demo02;
public class Application {
public static void main(String[] args) {
//类: 抽象的 实例化
//类实例化后返回一个自己的对象!
//student对象是一个Student类的具体实例
Student student = new Student();
student.name = "张三";
student.age = 18;
student.study();
}
}
package com.oop.demo02;
public class Person {
//一个类即使什么也不写,它也会存在一个方法
//显式定义构造器
String name;
//1.使用new关键字,本质实在调用构造器
//2.用来初始化值
public Person(){
}
//有参构造:一旦定义了有参构造,无参就必须显示定义
public Person(String name){
this.name = name;
}
//alt + insert
/*
* 构造器:
* 1.和类名相同
* 2.没有返回值
* 作用:
* 1.new本质在调用构造方法
* 2.初始化对象的值
* 注意点:
* 1.定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造器
*
*
* */
}
4.内存解析
![]()
5.封装
![]()
package com.oop.demo04;
public class Studnet {
//属性私有化
private String name; //姓名
private int id; //学号
private char sex;//性别
private int age;//年龄
//提供一些可以曹总该属性的方法
//get 获得这个属性
//set 为属性设置值
public String getName() {
return name;
}
public int getId() {
return id;
}
public char getSex() {
return sex;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 130 || age < 0){
this.age = 3;
}else{
this.age = age;
}
}
}
package com.oop.demo04;
/*
* 封装:
* 1.提高程序的安全性,保护数据
* 2.隐藏代码的实现细节
* 3.统一接口
* 4.系统可维护性增加了
* */
public class Application {
public static void main(String[] args) {
Studnet s1 = new Studnet();
s1.setName("scxlm");
s1.setAge(999);//不合法的
System.out.println(s1.getName());
System.out.println(s1.getAge());
}
}
6.什么是继承
![]()
Super
package com.oop.demo05;
import java.security.PublicKey;
//父类
public class Person {
public Person() {
System.out.println("父类的无参构造执行了");
}
protected String name = "kuangshen";
public void print(){
System.out.println("parent");
}
}
package com.oop.demo05;
//子类继承了父类,就会调用父类的全部方法!
public class Student extends Person{
public Student() {
//隐藏代码,调用了父类的无参构造
super();//调用父类的构造器,必须在子类构造器的第一行
System.out.println("子类的无参构造执行了");
}
public Student(String name) {
this.name = name;
}
public String name = "qinjiang";
public void test(String name){
System.out.println(name);//秦疆
System.out.println(this.name);//qinjiang
System.out.println(super.name);//qinjiang
}
//私有的东西无法被继承 private
public void print(){
System.out.println("student");
}
public void test1(){
print();//student
this.print();//student
super.print();//parent
}
}
package com.oop.demo05;
//在java中,所有的类,都默认直接或者间接继承object
public class Application {
public static void main(String[] args) {
Student student = new Student();
//student.test("秦疆");
// student.test1();
}
}
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现自子类的方法或者构造方法中!
3.super和this不能同时调用构造方法!
vs this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提:
this:没有继承也可以使用
super:只能在继承条件才可以使用
构造方法:
this();本类的构造
super();父类的构造
重写
package com.oop.demo05;
public class B {
public void Test(){
System.out.println("B--->Test");
}
}
package com.oop.demo05;
public class A extends B{
//重写都是方法的重写,和属性无关
@Override //注解:有功能的注释
public void Test(){
System.out.println("A--->Test");
}
}
package com.oop.demo05;
//在java中,所有的类,都默认直接或者间接继承object
public class Application {
public static void main(String[] args) {
//静态方法和非静态方法区别很大
//静态方法: 方法的调用只和左边定义的数据类型有关
//非静态:重写
A a = new A();
a.Test();//A--->Test
//父类的引用指向了子类
B b = new B();//子类重写了父类的方法
b.Test();//A--->Test
}
}
重写:需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数类型必须相同
3.修饰符:范围可以扩大,但不能缩小:public>Protected>Default>private
4.抛出的异常,范围可以缩小,但不能扩大:ClassNotFoundException --->Exception(大)
重写:子类的方法和父类必须一致,方法体不同
为什么需要重写:
1.父类的功能,子类不一定需要,或者不一定满足
Alt+Insert:Override
7.多态
![]()
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("Parent");
}
/*
多态的注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类有联系,类型转换异常!ClassCastException
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
1.static方法,属于类,它不属于实例
2.final常量:
3.private方法:
*/
}
package com.oop.demo06;
public class Student extends Person{
public void run(){
System.out.println("Son");
}
public void eat(){
System.out.println("eat");
}
}
package com.oop.demo06;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student
//new Person
//可以指向的引用类型就不确定了:父类的引用指向子类
//Studnet 能调用的方法都是自己的或者继承父亲的
Student s1 = new Student();
s1.run();
s1.eat();
//Person 可以指向子类,但是不能调用子类独有的方法
Person s2 = new Person();
s2.run();
((Student)s2).eat();
}
}
8.instancof
package com.oop.demo07;
public class Application {
public static void main(String[] args) {
//Object -->String
//Object -->Person -->Studnet
//Object -->Person -->Teacher
/* Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
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 Teacher);//false
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
// System.out.println(person instanceof String);//false
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 Teacher);
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
// System.out.println(person instanceof String);//false*/
//子类转换为父类,可能丢失自己的本来的一些方法
Student student = new Student();
student.go();
Person person = student;
}
/*
* 1.父类引用指向子类的对象
*2.把子类转换为父类,向上转型
* 3.把父类转换为子类,向下转型:强制转换
* 4.方便方法的调用,减少重复的代码
*
* */
}
9.Static
package com.oop.demo08;
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 person = new Person();
}
}
package com.oop.demo08;
//静态导入包
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(random());
System.out.println(PI);
}
}
10.抽象类
![]()
package com.oop.demo09;
public abstract class Action {
//抽象方法,只有方法名字,没有方法实现
public abstract void doSomething();
//1.不能new这个抽象类,只能靠子类去实现它,约束
//2.抽象类中可以写普通的方法
//3.抽象方法必须在抽象类中
//抽象的抽象,约束
}
package com.oop.demo09;
public class A extends Action{
@Override
public void doSomething() {
}
}
11.接口
![]()
package com.oop.demo10;
public interface TimerService {
void Timer();
}
package com.oop.demo10;
//interface 定义的关键字,接口都需要有实现类
public interface UserService {
//接口中所有定义的方法其实都是抽象的,abstract
//常量 public static final
int AGE=99;
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
package com.oop.demo10;
//抽象类 extends
//类 可以实现接口 implements 接口
//实现了接口中的类,就需要重新接口中的方法
//多继承,利用接口实现多继承
public class UserServiceImpl implements UserService,TimerService{
@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.必须要重新接口中的方法
12.内部类
![]()
13.异常
![]()
![]()
![]()
![]()
![]()
package com.excepton;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
//假设捕获多个异常,从小到大
try{//try 监控区域
System.out.println(a/b);
}catch (Error e){//catch(想要捕获的异常类型!)捕获异常
System.out.println("Error");
}catch (Exception e){
System.out.println("Exception");
}catch (Throwable e){
System.out.println("Throwable");
}finally {//finally 处理善后工作
System.out.println("finally");
}
}
}
package com.excepton;
public class Test2 {
public static void main(String[] args) {
try {
new Test2().test(1,0);
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
//假设这方法中,处理不了这个一异常,方法上抛出异常
public void test(int a,int b) throws ArithmeticException{
if (b == 0){
throw new ArithmeticException();//主动抛出异常,一般在方法中使用
}
}
}
自定义异常
![]()
package com.excepton.demo02;
//自定义异常类
public class MyException extends Exception{
//传递数字>10
private int detail;
public MyException(int a){
this.detail = a;
}
//异常打印信息
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
package com.excepton.demo02;
public class Test {
static void test(int a) throws MyException {
System.out.println("传递的参数为:"+a);
if (a > 10){
throw new MyException(a);//抛出
}
System.out.println("OK");
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
System.out.println("MyException=>"+e);
}
}
}
异常经验总结
![]()