Java学习第十三天<instance of><static 关键字详解><抽象类><接口的定义与实现><N中内部类>

instance of

package com.oop.demo06;
​
public class Teacher extends Person{
​
}

package com.oop.demo06;
​
public class Person {
    public void run(){
        System.out.println("run");
    }
}

package com.oop.demo06;
​
public class Studenr2 extends Person{
    public void go(){
        System.out.println("go");
    }
}
/*测试
 //Object>String
        //Object>Person>Teacher
        //Object>Person>Student2
        //System.out.println(x instanceof y);判断x与y是否存在父子关系 左边类名级别决定能跟哪一级编译判断,不能和并列级判断
        Object studenr1 = new Studenr2();
        System.out.println(studenr1 instanceof Studenr2);
        System.out.println(studenr1 instanceof com.oop.demo06.Person);
        System.out.println(studenr1 instanceof Object);//true
        System.out.println(studenr1 instanceof com.oop.demo06.Teacher);//false
        System.out.println(studenr1 instanceof String);
        System.out.println("===============================================");
       com.oop.demo06.Person person1 = new Studenr2();
        System.out.println(person1 instanceof Studenr2);
        System.out.println(person1 instanceof com.oop.demo06.Person);
        System.out.println(person1 instanceof Object);//true
        System.out.println(person1 instanceof com.oop.demo06.Teacher);//false
        //System.out.println(person1 instanceof String); 编译就报错
        System.out.println("===============================================");
        com.oop.demo06.Studenr2 student2 = new com.oop.demo06.Studenr2();
        System.out.println(student2 instanceof Studenr2);
        System.out.println(student2 instanceof com.oop.demo06.Person);
        System.out.println(student2 instanceof Object);//true
        //System.out.println(student2 instanceof com.oop.demo06.Teacher);//编译就报错
        //System.out.println(student2 instanceof String); //编译就报错
​
        //类型之间转化:基本类型 高低64 32 16 8 / 父 子
        //将person1这个对象转化为Studenr2类型,就可以使用Studenr2的方法了,高转低要强转
        //子类转化为父类,可能丢失一些方法
        ((Studenr2) person1).go();
 */
/*
1.父类引用指向子类对象,反之不行
2.子类转为父类,不用强转,会丢失方法
3.父类转为子类,要强转
4.方便方法调用,减少重复代码,不用再重新new一个类,把原来的降级或升级可以调用其他方法
面向对象特征:封装、继承、多态 都是为了抽象(编程思想)
 */

static 关键字详解

package com.oop.demo07;
//static
public class Student {
    private static int age;//静态变量,能被多个类共享,多线程
    private double score;//非静态变量
​
    public void run(){//非静态方法可调用静态方法所有东西
        go();
    }
    public static void go(){
​
    }
​
    public static void main(String[] args) {
        Student s1 = new Student();
        System.out.println(Student.age);//类变量(静态变量推荐用类名.变量,非静态变量(score)不能这样访问)
        System.out.println(s1.age);//对象调用变量
        System.out.println(s1.score);
​
        new Student().run();//静态方法可调用静态方法,不能调用普通方法(静态与类一起加载,加载之前没有普通类),需要new
        Student.go();//go();
​
​
    }
​
}

package com.oop.demo07;
​
public class Person {
    {
        //2.代码块(匿名代码块)构造器之前,用来赋初始值
        System.out.println("匿名代码块");
    }
    static{
        //1.静态代码块,加载初始化数据,与类一起加载,只执行一次
        System.out.println("静态代码块");
    }
​
    public Person() {//3.构造器
        System.out.println("构造方法");
    }
​
    public static void main(String[] args) {
        Person person1 = new Person();//调用非静态加new
        System.out.println("===================");
        Person person2 = new Person();//static只执行一次
​
    }
}

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);
    }
}
//final修饰类 无父子关系

抽象类

package com.oop.demo08;
//抽象类 本质是类需要继承 extend:单继承  (接口可以多继承)
public abstract class Action {
    //只有约束~不写方法体,有人帮我们实现
    public abstract void doSomething();//抽象方法,只有方法名,没有方法实现
​
​
}
/*抽象类
1.不能new,只能靠子类实现
2.抽象类可以写普通方法,可以写抽象方法,但有了抽象方法必包含于抽象类
3.抽象的抽象:约束
思考题
1.存在构造器吗,存在
2.存在的意义是什么 抽象共有属性,重写方法,提高开发效率,可扩展性
 */

package com.oop.demo08;
//抽象类的所有方法必须要由子类实现,除非子类也抽象,需要子子类实现
public class A extends Action{
​
    @Override
    public void doSomething() {
​
    }
}

接口的定义与实现

package com.oop.demo09;
//锻炼抽象思维
//接口 interface定义关键词 接口都需要实现类
public interface UserService{
    //接口中所有定义的方法都是抽象的 简写 返回值类型+方法名
    void run(String name);//public abstract void run();
    void delete(String name);
    void update(String name);
    void query(String name);
    //接口中所有定义的属性都是常量 public static final 
    int AGE=99;
    
    
}

package com.oop.demo09;
​
public interface TimeService {
    void timer();
​
}

package com.oop.demo09;
//类可以实现接口 implements接口(多个)  extends抽象类(单继承)
//实现接口,需要重写接口中方法
public class UserServiceImpl implements UserService,TimeService{//alt+回车
​
    @Override
    public void run(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.定义一些方法,让不同的人实现
3.方法都是 public abstract
4.常量都是 public static final
5.接口不能被实例化,接口中没有构造方法(接口不是类,不能new)
6.implements可实现多个接口
7.必须要重写接口中方法

N中内部类

成员内部类、静态内部类、局部内部类、匿名内部类

package com.oop.demo10;
​
public class Outer {
​
    private int id=10;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    public class Inner{//加static变静态内部类,与类一起加载
        public void in(){
            System.out.println("这是内部类的方法");
        }
        //内部类获得外部类的私有属性
        public void ID(){
            System.out.println(id);
        }
    }
​
    public void method(){
        class Inner2{ //局部内部类
            public void in(){
​
            }
        }
    }
}
//一个java类中有多个class类,但只能有一个public class类
class A {
    public static void main(String[] args) {
​
    }
}
/*
 Outer outer = new Outer();
        outer.out();
        //通过外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.in();
        inner.ID();
 */

package com.oop.demo10;
​
public class Test {
    public static void main(String[] args) {
        new Apple().eat();//没有名字初始化类,不用把实例保存到变量中
        new UserService() {//匿名内部类
            @Override
            public void hello() {
​
            }
        };
    }
}
class Apple{
    public void eat(){
        System.out.println("1");
    }
}
interface UserService{
     void hello();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值