面向对象
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 {
接口的定义与实现
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 {
内部类
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(){
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: 异常的打印信息
mian方法传递参数
浙公网安备 33010602011771号