面向对象

static关键字详解

package com.oop.demo07;
//static
public class Person {
   //1.只执行一次
   static {
       System.out.println("静态代码块");
  }
   //2.赋初值
  {
       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.demo07;
​
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();
​
      Student student = new Student();
//       System.out.println(Student.age);
//       System.out.println(student.age);
//       System.out.println(student.score);
       student.go();
  }
​
}
package com.oop.demo07;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
   public static void main(String[] args) {
       System.out.println(random());
       System.out.println(PI);
  }
}
​

抽象类

package com.oop.demo08;
​
package com.oop.demo08;
​
public class A extends Action {
   @Override
   public void doSomething() {
​
  }
}
​
//abstract 抽象类: 类 extends: 单继承 (接口多继承)
public abstract class Action {
   //约束 有人帮我们实现
   //abstract, 抽象方法,只有方法名字,没有方法的实现
   public abstract void doSomething();
​
   //1.不能new这个抽象类,只能靠子类去实现它:约束
   //2.抽象类中可以写普通的方法
   //3.抽象方法必须在抽象类中
   //抽象的抽象: 约束
   //思考题? abstract不能new,存在构造器吗?
               //存在的意义 抽象出来 提高开发效率
}
​
​

接口的定义与实现

package com.oop.demo09;
​
//抽象的思想 -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);
}
package com.oop.demo09;
​
public interface TimeService {
   void timer();
}
package com.oop.demo09;
//抽象类 :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 timer() {
​
  }
​
   @Override
   public void query(String name) {
​
  }
}
作用:
   1.约束
   2.定义一些方法,让不同的人实现 10--》1
   3.接口中的方法 public abstract
   4.接口中的属性 public static final
   5.接口不能被实例化,接口中没有构造方法
   6.implements 可以实现多个接口
   7.必须要重写接口中的方法
   8.总结博客

内部类

package com.oop.demo10;
​
public class Outer {
   private int id ;
   public void Outer(){
       System.out.println("这是外部类的方法");
  }
   //static 静态内部类
   //在方法中写class 局部内部类
   public  class  Inter{
       public void in(){
           System.out.println("这是内部类的方法");
      }
       //获得外部类的私有属性
       public  void getId(){
           System.out.println(id);
      }
  }
}
package com.oop.demo10;
​
​
​
//匿名内部类
public class Test {
   public static void main(String[] args) {
       //Apple a = new Apple();
       //没有名字初始化类,不用将实例保持在变量中
       new Apple().eat();
       new UserService(){
           @Override
           public void hello() {
​
          }
      };
  }
}
class Apple{
   public void eat(){
       System.out.println(1);
  }
}
interface UserService {
   void hello();
}
public class Application {
   public static void main(String[] args) {
       Outer outer = new Outer();
       //通过这个外部类来实例化内内部类
       Outer.Inter inner = outer.new Inter();
       inner.in();
       inner.getId();
  }
}

Error和Exception

package com.rxception.demo01;
​
public class Demo01 {
   public static void main(String[] args) {
       System.out.println(11/0);
//       new Demo01().a();
  }
//   public void a(){
//       b();
//   }
//   public void b(){
//       a();
//   }
}
package com.rxception.demo01;
//假设要捕获异常,要从小到大
public class Test {
   public static void main(String[] args) {
​
       int a = 1;
       int b = 0;
       //System.out.println(a/b);
       try {//try监控区域
           new Test().a();
           System.out.println(a/b);
      }catch (Error e){//catch(想要捕获的异常类型) 捕获异常
           System.out.println("程序出现异常,变量b不能为0");
      }catch (Exception e){//catch(想要捕获的异常类型) 捕获异常
           System.out.println("Exception");
      }catch (Throwable e){//catch(想要捕获的异常类型) 捕获异常
           System.out.println("Throwable");
      }
       finally {//处理善后工作
           System.out.println("finally");
      }
​
       //finally 可以不要finally, 假设IO,资源,关闭
  }
   public void a(){b();}
   public void b(){a();}
}
package com.rxception.demo01;
​
public class Test2 {
   public static void main(String[] args) {
​
       try {
           new Test2().test(1,0);
      } catch (ArithmeticException e) {
           e.printStackTrace();
      } finally {
      }
  }
   //假设这方法中,处理不了这个异常,方法上抛出异常
   public void test(int a , int b ) throws ArithmeticException {
       if (b==0){//主动抛出异常 throw throws 一般在方法中使用
              throw new ArithmeticException();
        }
       // System.out.println(a/b);
  }
​
}
​
//       int a = 1;
////       int b = 0;
////       //Ctrl + Alt + t
////       try {
////           if (b==0){//主动抛出异常 throw throws
////               throw new ArithmeticException();
////           }
////           System.out.println(a/b);
////       } catch (Exception e) {
////           System.exit(-1);
////           e.printStackTrace();//打印错误的栈信息
////       } finally {
////       }

自定义异常

package com.rxception.demo02;
​
//自定义的异常类
public class MyExcepttion extends Exception {
   //传递数字>10
   private int detail;
​
   public MyExcepttion(int a) {
       this.detail = a;
  }
   //to String: 异常的打印信息
​
   @Override
   public String toString() {
       return "MyExcepttion{" +
               "detail=" + detail +
               '}';
  }
}
public class Test {
   //可能会存在异常的方法
   static void test(int a) throws MyExcepttion{
       System.out.println("传递的参数为:" +a);
       if (a > 10){
           throw new MyExcepttion(a);//抛出
      }
       System.out.println("OK");
  }
​
   public static void main(String[] args) {
       try {
           test(11);
      } catch (MyExcepttion e) {
           //增加一些处理异常的代码块
           System.out.println("MyExcepttion-->" +e);
      } finally {
      }
  }
}
​

mian方法传递参数

 

posted on 2021-05-07 09:41  lsys  阅读(226)  评论(0)    收藏  举报

导航