面向对象编程

面向对象编程(Object-Oriented Programming)

Demo01 java都是值传递

package com.oop.demo01;
​
import java.io.IOException;
​
//Demo01  类
public class Demo01 {
    //main 方法
    public static void main(String[] args) {
​
    }
​
    /*
    修饰符  返回值类型  方法名(...){
        //
        return 返回值;
    }
    */
    //return  结束方法,返回一个结果
    public String sayHello(){
        return "hello,world";
    }
​
    public void hello(){
        return;
    }
​
    public int max(int a,int b){
        return a>b ? a : b;  //三元运算符
    }
​
    //数组下标越界:ArrayIndexOutOfBounds
    //异常抛出
​
    public void readFile(String file) throws IOException {
​
    }
}
​
​
package com.oop.demo01;
​
public class Demo02 {
    //静态方法 static
    public static void main(String[] args) {
        //实例化这个类 new
        //对象类型  对象名 = 对象值;
​
       Student student = new Student();
       student.say();
    }
​
    //static和类一起加载的
    public void a(){
        b();
    }
​
    //没有static就是类实例化之后才存在
    public void b(){
​
    }
}
​
​
package com.oop.demo01;
​
public class Demo03 {
    public static void main(String[] args) {
        //实际参数和形式参数的类型要对应!
       int add = Demo03.add(1,2);
        System.out.println(add);
    }
​
    public static int add(int a,int b){
       return a+b;
    }
}
​
​
package com.oop.demo01;
​
//值传递
public class Demo04 {
    public static void main(String[] args) {
       int a = 1;
        System.out.println(a);  //1
​
        Demo04.change(a);
​
        System.out.println(a);  //1
    }
​
    //返回值为空
    public static void change(int a){
        a = 10;
    }
}
​
​
package com.oop.demo01;
​
//引用传递:对象,本质还是值传递
public class Demo05 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);  //null
​
        Demo05.change(person);
        System.out.println(person.name);  //晨曦
    }
​
    public static void change(Person person){
        //person是一个对象,指向的是---> Person person = new Person();这是一个具体的人,可以改变属性。
        person.name = "晨曦";
    }
}
​
//定义了一个Person类,有一个属性:name
class Person{
    String name;  //null
}
​
​
package com.oop.demo01;
​
//学生类
public class Student {
​
    //方法
    public static void say(){
        System.out.println("学生说话了");
    }
}

Demo02 类的实例化,new object

package com.oop.demo02;
​
//java---->class
public class Person {
    //一个类即使什么也不写,它也会存在一个方法
    //显示的定义构造器
​
    String name;
    int age;
​
​
    //实例化初始化值
    //1.使用new关键字,本质是在调用构造器
    //2.用来初始化值
    public Person(){
        this.name = "chenchen";
    }
​
    //有参构造:一旦定义了有参构造,无参就必须显示定义
    public Person(String name){
        this.name = name;
    }
​
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }
}
​
​
/*
public static void main(String[] args) {
​
        //new 实例化了一个对象
        Person person = new Person("chenxi");
​
        System.out.println(person.name);  //null
    }
    构造器:
    1.和类名相同
    2.没有返回值
    构造器的作用:
    1.使用new关键字,本质是在调用构造器
    2.用来初始化对象的值
    注意点:
    1.定义有参构造后,如果使用无参构造,显示的定义一个无参的构造
​
    alt+insert后选择"Constructor"  生成构造函数的快捷键
​
    this. = value;
    this. 代表当前类的
    = value是参数传进来的值;
 */
​
​
package com.oop.demo02;
​
public class Application {
​
    public static void main(String[] args) {
        //类,抽象的,实例化
        //类实例化后会返回一个自己的对象
        //student对象就是一个Student类的具体实例
​
        Student xiaoming = new Student();
        Student xh = new Student();
​
        xiaoming.name = "小明";
        xiaoming.age = 3;
        xh.name = "小红";
        xh.age = 3;
​
        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);
        System.out.println(xh.name);
        System.out.println(xh.age);
    }
}
​
​
package com.oop.demo02;
​
//学生类
public class Student {
​
    //属性:字段
    String name;  //默认值为null
    int age;      //默认值为0
​
    //方法
    public void study(){
        System.out.println(this.name+"在学习");
    }
​
}
​
//学程序? 对世界进行更好的建模!
​
​
package com.oop.demo02;
​
//一个项目应该只存在一个main方法
public class App02P {
    public static void main(String[] args) {
​
        //new  实例化了一个对象
        Person person = new Person("chenxi");
        System.out.println(person.name);  //chenchen
​
    }
}

Demo03 类与对象

package com.oop.demo03;
​
public class Pet {
    public String name;  //name:null
    public int age;      //age:0
​
    //无参构造
​
    public void shout(){
        System.out.println("叫了一声");
    }
}
​
​
package com.oop.demo03;
​
import com.oop.demo03.Pet;
​
public class Application {
    public static void main(String[] args) {
​
        /* 1.类与对象
            类是一个模板:抽象,对象是一个具体的实例
​
           2.方法
             定义、调用
​
           3.对象的引用
             引用类型:基本类型(8个基本类型)
             对象是通过引用来操作的: 栈----> 堆
​
           4.属性:字段Field  成员变量
             默认初始化:
             数字:  0  0.0
             char:  u0000
             boolean: false
             引用:    null
             修饰符  属性类型  属性名 = 属性值!
​
            5.对象的创建和使用
              - 必须使用new关键字创造对象,构造器  Person chen = new Person();
              - 对象的属性  chen.name
              - 对象的方法  chen.sleep()
​
            6.类
              静态的属性   属性
              动态的行为   方法
​
            7.封装、继承、多态
         */
​
        Pet dog = new Pet();
        dog.name = "旺财";
        dog.age = 3;
        dog.shout();
​
        System.out.println(dog.name);
        System.out.println(dog.age);
​
        Pet cat = new Pet();
    }
}

Demo04 public 的 get、set方法

package com.oop.demo04;
​
//类   private:私有
public class Student {
    //属性私有
    private String name;   //名字
    private int id;       //学号
    private char sex;    //性别
    private int age;    //年龄
​
    //提供一些可以操作这个属性的方法!
    //提供一些 public 的 get、set方法
​
    //get 获得这个数据
    public String getName(){
        return this.name;
    }
​
    //set 给这个数据设置值
    public void setName(String name){
        this.name = name;
    }
​
    //alt + insert键
    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;
    }
​
    public int getAge() {
        return age;
    }
​
    public void setAge(int age) {
        if (age > 120 || 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) {
      Student s1 = new Student();
      s1.setName("chenchen");
​
      //方法名,参数列表相同 就是同一个方法
        System.out.println(s1.getName());
​
        s1.setAge(299);   //不合法的数据
        System.out.println(s1.getAge());
    }
}

Demo05 继承

package com.oop.demo05;
​
//在Java中,所有的类,都默认直接或者间接继承object类
//Person 人 :父类
public class Person /*extends Object*/ {
    //public
    //protected
    //default
    //private
​
    public int money = 10_0000_0000;
    //private int money = 10_0000_0000;
​
    public void say(){
        System.out.println("说了一句话");
    }
​
    public int getMoney(){
        return money;
    }
    public void setMoney(int money){
        this.money = money;
    }
​
}
​
​
package com.oop.demo05;
​
//学生  is  人 :派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person{
​
    //ctrl+h  继承树快捷键
}
​
​
package com.oop.demo05;
​
//Teacher  is  人 :派生类,子类
public class Teacher extends Person{
​
}
​
​
package com.oop;
​
import com.oop.demo05.Person;
import com.oop.demo05.Student;
​
public class Application {
    public static void main(String[] args) {
​
       Student student = new Student();
       student.say();
       System.out.println(student.money);
​
    }
}

Demo06 继承

package com.oop.demo06;
​
//在Java中,所有的类,都默认直接或者间接继承 Object类
//Person  人 : 父类
public class Person /*extends Object*/{
​
    public Person(String name) {
        System.out.println("Person无参执行了");
    }
​
    protected String name = "chenxi";
​
    //私有的东西无法被继承!
    public void print(){
        System.out.println("Person");
    }
​
}
​
​
package com.oop.demo06;
​
//学生  is  人 : 派生类,子类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person{
​
    public Student() {
        //隐藏代码:调用了父类的无参构造
        super("name");  //调用父类的构造器,必须要在子类构造器的第一行
        //子类可以调用父类的有参,但要显示地去调用;如果不写,默认的调用是父类的无参
​
        System.out.println("Student无参执行了");
    }
​
    private String name = "chenchen";
​
    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);
        System.out.println(super.name);
    }
}
​
​
package com.oop.demo06;
​
/*super注意点:
   1. super调用父类的构造方法,必须在构造方法的第一个
   2. super 必须只能出现在子类的方法或者构造方法中!
   3. super 和 this 不能同时调用构造方法!
​
 与 this 的区别:
    this:   代表本身调用者这个对象
    super:  代表父类对象的引用
​
 前提:
   this:   没有继承也可以使用
   super:  只能在继承条件下才可以使用
 构造方法:
   this();   本类的构造
   super();  父类的构造
 */
​
public class Application {
    public static void main(String[] args) {
​
        //Person person = new Person();
        Student student = new Student();
        //student.test("晨陈");
        //student.test1();
​
    }
}

Demo07 方法的重写

package com.oop.demo07;
​
//重写都是方法的重写,和属性无关
public class B {
​
    public void test(){
        System.out.println("B=>test()");
    }
}
​
​
package com.oop.demo07;
​
//继承
public class A extends B{
​
    //Override  重写
    @Override  //注解:有功能的注释!
    public void test() {
        System.out.println("A=>test()");
    }
}
​
​
package com.oop.demo07;
​
/*重写:需要有继承关系,子类重写父类的方法
    1. 方法名必须相同
    2. 参数列表必须相同
    3. 修饰符:范围可以扩大但不能缩小:  public > protected > default > private
    4. 抛出的异常: 范围,可以被缩小,但不能扩大:ClassNotFoundException --> Exception(大)
 重写,子类的方法和父类必须要一致;方法体不同
​
 为何需要重写?
   1. 父类的功能,子类不一定需要,或者不一定满足
   alt + insert键 :选择 override;
 */
public class Application {
​
    //静态的方法 和 非静态的方法 区别很大
    //静态方法:方法的调用只和左边定义的数据类型有关
    //非静态方法: 才有才可以重写
​
    public static void main(String[] args) {
        A a = new A();
        a.test();  //A
​
        //父类的引用指向了子类
        B b = new A();
        b.test();  //B
    }
}

Demo08 多态

package com.oop.demo08;
​
/*多态注意事项:
  1. 多态是方法的多态,属性没有多态。
  2. 父类和子类之间有联系。  类型转换异常:ClassCastException!
  3. 存在条件:有继承关系;方法需要被重写;父类引用指向子类对象!  Father f1 = new Son();
​
 不能重写的方法有:
   1. static 方法,属于类,它不属于实例
   2. 被 final 修饰的,常量;
   3. 被 private 修饰的 方法;
 */
​
public class Person {
​
    public void run() {
        System.out.println("run");
    }
​
}
​
​
package com.oop.demo08;
​
public class Student extends Person{
​
    @Override
    public void run() {
        System.out.println("son");
    }
​
    public void eat(){
        System.out.println("eat");
    }
}
​
​
package com.oop.demo08;
​
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();
​
        //对象能执行那些方法,主要看对象左边的类型,和右边关系不大!
        s2.run();  //子类重写了父类的方法,执行子类的方法
        s1.run();
        s1.eat();
        //s2.eat();
        ((Student) s2).eat();   //把 Person类型从高到低 强制转换为 Student类型
    }
}

Demo09 对象类型转换

package com.oop.demo09;
​
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");
    }
}
​
/*
 //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Object object = new Student();
​
        //System.out.println(X instanceof 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);   //编译就报错!
        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);  //编译就报错
 */
​
​
package com.oop.demo09;
​
public class Teacher extends Person{
​
}
​
​
package com.oop.demo09;
​
import java.util.Scanner;
​
public class Application {
​
    public static void main(String[] args) {
        //类型之间的转换:  父类   子类
        //高                  低
        //Person student = new Student();
        //student将这个对象转换为Student类型,我们就可以使用Student类型的方法!
        //((Student)student).go();
​
        //子类转换为父类,可能丢失自己的本来的一些方法!
        Student student = new Student();
        student.go();
        Person person = student;
        //person.go();
​
    }
}
​
/* 1. 父类引用指向子类的对象
   2. 把子类转换为父类,向上转型;
   3. 把父类转换为子类,向下转型;   强制类型转换
   4. 方便方法的调用,减少重复的代码! 简洁
​
   封装、继承、多态    抽象类,接口
 */

Demo10 代码块

package com.oop.demo10;
​
/*  {
        //代码块(匿名代码块)
    }
    static{
        //静态代码快
        }
 */
​
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 person1 = new Person();
        System.out.println("===============");
        Person person2 = new Person();
    }
​
}
​
​
package com.oop.demo10;
​
//static关键字
public class Student {
​
    private static int age;  //静态的变量   多线程!
    private double score;    //非静态的变量
​
    public void run(){ //非静态方法
​
    }
    public static void go(){ //静态方法static和类一起加载
​
    }
​
    public static void main(String[] args) {
        /*Student s1 = new Student();
​
        System.out.println(Student.age);  //类名调用变量
        //System.out.println(Student.score);  非静态变量不能使用类名去调用
        System.out.println(s1.age);
        System.out.println(s1.score);*/
​
        new Student().run();
        Student.go();
        go();
    }
}
​
​
package com.oop.demo10;
​
//被 final 所修饰的类,它不能被继承,没有子类了
//静态导入包
import static java.lang.Math.random;  //random:每次生成一个随机数
import static java.lang.Math.PI;
​
public class Test {
    public static void main(String[] args) {
        System.out.println(random());
        System.out.println(PI);    //3.1415926
    }
}

Demo11 抽象类

package com.oop.demo11;
​
//abstract 抽象类:本质是类
//extends:单继承~   (接口可以多继承)
public abstract class Action {
​
    //约束~  有人帮我们实现!!
    //abstract,抽象方法,只有方法名字,没有方法的实现!
    public abstract void doSomething();
​
/*抽象类的特点:
  1. 不能new这个抽象类,只能靠子类去实现它;约束~~
  2. 抽象类中可以写普通的方法~
  3. 抽象方法必须在抽象类中~
  4. 抽象的抽象:约束~
 */
​
    //思考题?   1.抽象类不能new对象,那么它存在构造器吗?   没有构造器
    //          2.抽象类存在的意义?   提高开发效率,增加了可扩展性
}
​
​
package com.oop.demo11;
​
//抽象类的所有方法,继承了它的子类 都必须要 实现它的方法!   --->> 除非继承了它的子类也是抽象类
public class A extends Action{
    @Override
    public void doSomething() {
​
    }
}

Demo12 接口

package com.oop.demo12;
​
//锻炼增强~抽象的思维!
//interface --> 定义的关键字;  接口都需要有实现类
public interface UserService {
​
    //在接口中定义的常量是静态常量,默认用 public static final 修饰
    int age = 99;
​
    //接口中的所有定义的方法 其实都是抽象的;  默认用 public abstract修饰,因此可以不写
    public abstract void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}
​
​
package com.oop.demo12;
​
//抽象类: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() {
​
    }
}
​
​
package com.oop.demo12;
​
/*接口 的作用:
   1. 接口是一个 约束!!!
   2. 定义一些方法,让不同的人实现~~
   3. 方法都是 public abstract 所修饰的
   4. 常量都是 public static final 所修饰的
   5. 接口不能被直接实例化~,接口中没有构造方法
   6. implements 可以实现多个 接口
   7. 实现接口,必须要重写 接口中的 方法
 */
​
public interface TimeService {
    void timer();
}

Demo13 内部类

package com.oop.demo13;
//成员内部类
public class Outer {
​
    private int id=1;
    public void out(){
        System.out.println("这是外部类的方法");
    }
​
    public class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
        //获得外部类的私有属性~
        public void getID(){
            System.out.println(id);
        }
    }
​
}
​
​
package com.oop.demo13;
​
public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        //通过这个外部类来实例化内部类~
        Outer.Inner inner = outer.new Inner();
        inner.in();
        inner.getID();
    }
}
​
​
package com.oop.demo13;
//静态内部类
public class Outer01 {
    private int id = 10;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    //static静态内部类无法直接访问 非静态的属性
    public static class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
        //获得外部类的私有属性
        public void getID(){
            System.out.println(id);
        }
    }
}
​
​
package com.oop.demo13;
​
public class Application01 {
    public static void main(String[] args) {
        Outer01 outer01 = new Outer01();
        //通过这个外部类来实例化内部类
        Outer01.Inner inner = outer01.new Inner();  //直接报错,static静态内部类无法直接访问 非静态的属性
        inner.getID();
    }
}
​
​
package com.oop.demo13;
​
public class Outer02 {
​
}
​
//java类中可以有多个class类,但只能有一个public class
class A{
    public static void main(String[] args) {
​
    }
}
​
​
package com.oop.demo13;
​
public class Outer03 {
    //局部内部类:
    public void method(){
​
        class Inner{
            public void in(){
​
            }
        }
    }
}
​
​
package com.oop.demo13;
​
public class Test {
    public static void main(String[] args) {
        //没有名字初始化类,不用将实例保存到变量中~
        new Apple().eat();
​
        UserService userService = new UserService(){//这个方法其实会返回一个 userService对象,这个new UserService类是默认的,它就是实现了这个接口 的类,它没有名字,因此叫做匿名内部类!!!
            @Override
            public void hello() {
​
            }
        };
    }
}
​
class Apple{
    public void eat(){
        System.out.println("123,开始吃苹果!");
    }
}
​
interface UserService{
    void hello();
}

Exception

Demo01

package com.exception.demo01;
​
public class Demo01 {
    public static void main(String[] args) {
        //new Demo01().a();  递归调用形成循环依赖,让程序出错
        //System.out.println(11/0);  //ArithmeticException: / by zero,被除数不能为0
    }
​
    /*public void a(){
        b();
    }
    public void b(){
        a();
    }*/
}
​
​
package com.exception.demo01;
​
public class Test {
    public static void main(String[] args) {
​
        int a = 1;
        int b = 0;
​
        try { //try 监控区域
            System.out.println(a / b);
        } catch (ArithmeticException e) { //catch  捕获异常
            System.out.println("程序出现异常,变量b不能为0");
        }finally{ //处理善后工作
            System.out.println("finally");
        }
​
        //try{ }catch(){ }结构是必须要的东西
        //finally:可以不要finally区域。  假设IO流(Scanner对象),资源等一些东西需要关闭,我们可以把那些关闭的操作放在finally区域里面。
​
    }
​
}
​
​
package com.exception.demo01;
​
public class Test1 {
    public static void main(String[] args) {
​
        int a = 1;
        int b = 0;
​
        //假设要捕获多个异常:从小到大!   Error/Exception--->Throwable
​
        try{ //try,监控区域
            System.out.println(a/b);
        }catch(Error e){ //catch(catch里面的参数是想要捕获的异常类型),捕获异常
            System.out.println("Error");
        }catch(Exception e){
            System.out.println("Exception");
        }catch(Throwable t){
            System.out.println("Throwable");
        }finally{ //处理善后工作
            System.out.println("finally");
        }
    }
​
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}
​
​
package com.exception.demo01;
​
public class Test2 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
​
        //ctrl + alt + t 快捷键生成 try...catch...finally{}
        try {
            System.out.println(a/b);
        } catch (Exception e) {
            e.printStackTrace();  //打印错误的栈信息
        } finally {
        }
    }
}
​
​
package com.exception.demo01;
​
public class Test3 {
    public static void main(String[] args) {
​
        //抛出异常,就需要捕获处理,这样程序就可以继续运行
        try {
            new Test3().test(1,0);  //代码体
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }
​
    //假设这个方法中,处理不了这个异常,我们需要把异常抛出去(抛到更高级去),这就是在方法上抛出异常。
    public void test(int a,int b) throws ArithmeticException{
        if (b==0){
            throw new ArithmeticException();  //throw:主动的抛出异常,一般在方法中使用
        }
​
    }
}

Demo02

package com.exception.demo02;
​
/*  实际应用中的经验总结:
  1. 处理运行异常时,采用逻辑去合理规避,同时辅助try-catch处理
  2. 在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常
  3. 对于不确定的代码,也可以加上try-catch处理潜在的异常
  4. 尽量去处理异常,切记只是简单地调用printStackTrace()去打印输出
  5. 具体如何处理异常,要根据不同的业务需求和异常类型去决定
  6. 尽量添加finally语句块去释放占用的资源(比如IO流)
 */
​
//自定义的异常类
public class MyException extends Exception{
​
    //传递数字  >10
    private int detail;
​
    public MyException(int a) {
        this.detail = a;
    }
​
    //toString:异常的打印信息
    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}
​
​
package com.exception.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);
        }
    }
​
}
 
posted @ 2022-10-21 20:45  Chenshishu  阅读(21)  评论(0编辑  收藏  举报