Day12-instanceof和类型转换-static静态方法和变量-抽象-接口-异常-奇葩的类
-
判断是否有父子关系(类似布尔类型)
-
Father类
package day11;
public class Father {
public void say(){
System.out.println("Father");
}
public void name(){
System.out.println("11111");
}
}
-
Son类
package day11;
public class Son extends Father {
@Override
public void say() {
System.out.println("son");
}
public void sex(){
System.out.println("男");
}
}
-
Daugther
package day11;
public class Daugther extends Father {
public void say(){
System.out.println("daugther");
}
}
-
应用类
package day11;
public class Application {
public static void main(String[] args) {
//Father > Son
//Father > Daugther
Son s1 = new Son();
System.out.println(s1 instanceof Father);//TRUE
System.out.println(s1 instanceof Object);//TRUEs
System.out.println("========");
Father d1 = new Daugther();
System.out.println(d1 instanceof Father);//TRUE
System.out.println(d1 instanceof Object);//TRUE
System.out.println(d1 instanceof Daugther);//TRUE
System.out.println(d1 instanceof Son);//False
//强制转换
//父转子,强制转
Father s2 = new Son();
Son s3 = (Son) s2; //高转低,强制转
s3.sex();//男
//子转父,自动转
Father s5 = new Son();
s5.say();//Son,被子类方法重写了
//s5.sex();不能使用子类的独有的方法,错误
s5.name();//能使用父类独有的方法
}
static静态方法和变量
-
静态方法可以用类直接调用而不用对象,而非静态的话需要新建一个对象才可以调用
-
非静态方法可以调用静态方法,静态方法可以调用静态方法
-
静态方法是和类一起加载到内存的
-
例子
public class Test01 {
static int age;
String name;
public static void say(){
System.out.println("helloworld");
}
public void ok(){
System.out.println(111);
}
public static void main(String[] args) {
say();//静态可直接在类中调用方法
Test01 test = new day11.Test01();
test.ok();//非静态则需要新建对象
System.out.println(age);//直接可以用
System.out.println(test.name);//需要对象
}
}//输出
helloworld
111
0
null
-
执行顺序
package day11;
public class Test02 {
{
//代码块
System.out.println("我是第二个执行");
}
static {
//静态代码块
System.out.println("我是第一个执行且只执行一次");
}
public Test02() {
//构造方法
System.out.println("第三个执行");
}
public static void main(String[] args) {
Test02 t3 = new Test02();
System.out.println("=============");
Test02 t5 = new Test02();
}
}
执行结果:

抽象
-
抽象方法是要子类去重写的,他不能new出来,只能靠子类来实现
-
抽象类可以写普通方法
-
抽象方法必须在抽象类里
-
抽象类存在构造器
-
抽象方法没有“{}”
接口
-
约束
-
定义不同的方法,让不同的人去实现
-
public abstract //(接口里的每个方法都省略掉)
-
public static final //(接口的定义的常量省略掉的)
-
接口不能呗实例化,而且没有构造方法
-
接口可以多继承,implements去实现
-
public interface GaiLian {
} //接口格式 -
接口里的方法必须要实现,不然会报错
public interface GaiLian {
int age = 10;
void say();
String name();
}
public interface JieKou2 {
void say();
int sex();
}
public class GaiLianImp implements GaiLian, JieKou2 {
@Override
public void say() {
System.out.println("sot");
}
@Override
public int sex() {
return 0;
}
@Override
public String name() {
return null;
}
public static void main(String[] args) {
GaiLianImp gaiLianImp = new GaiLianImp();
gaiLianImp.say();
System.out.println(age);
}
}//输出
sot
10
奇葩的类
package day11;
public class QiPa {
public static void say(){
System.out.println(1);
class Hh{
public void in(){
}
}//局部内部类
}//外部类了
public static class Gg implements Cc{
void say(){
System.out.println(2);
}
@Override
public void Do() {
System.out.println("学习");
}
}//成员内部类
}
class Da{
int age(){
return 99;
}
public void sayAge(){
int a = age();
System.out.println(a);
}
}//内部类
interface Cc{
String name = "张三";
void Do();
}
package day11;
public class QiPaTest {
public static void main(String[] args) {
QiPa q1 = new QiPa();
q1.say();//外部类 输出: 1
QiPa.Gg g1 = new QiPa.Gg();
g1.say();//2
g1.Do();//学习
//内部类可以不用写名字初始化类
new Da().age();//99
Da da = new Da();
da.sayAge();//99
}
}
异常
1.
public class NumExcn {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
//监控
System.out.println(a/b);
} catch (Exception e) {
//catch 捕获异常
e.printStackTrace();
} finally {
//怎么都要执行的(通常用来善后)
System.out.println("finally");
}
System.out.println("HelloWorld");
}
}//输出
java.lang.ArithmeticException: / by zero
at yichang.NumExcn.main(NumExcn.java:12)
finally
HelloWorld
2.
public class NumExcn {
public static void main(String[] args) {
int a = 1;
int b = 0;
new NumExcn().num(1, 0);//程序已经停止,用try{}catch(){}捕捉到才可以继续执行下面的println语句
System.out.println("HelloWorld");
}
public void num(int a, int b){//throws (假设方法处理不了异常,就在方法上抛出)
if (b == 0){
throw new ArithmeticException();//主动抛出异常,在方法里使用
}
}
}//输出
Exception in thread "main" java.lang.ArithmeticException
at yichang.NumExcn.num(NumExcn.java:19)
at yichang.NumExcn.main(NumExcn.java:10)
自定义异常
//自定义的异常类
public class MyException extends Exception{
private int detail;
public MyException(int a) {
this.detail = a;
}
//异常的打印信息
@Override
public String toString() {
return "MyException{" + detail + '}';
}
}
public class Test {
//可能存在异常的方法
static void test(int a) throws MyException {
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("自定义异常");
}
}
}
浙公网安备 33010602011771号