java的抽象 ,接口

package com.lol.LiOu.oop;

import com.lol.LiOu.oop.Demo03.Pet;

//一个项目应该只存在一个main方法
public class Application {
    public static void main(String[] args) {

        //类:抽象的,实例化
        //类实例化后会返回一个自己的对象
        //student对象就是一个Student类的具体事例

//        Student xiaoming = new Student();
//        Student xh = new Student();
//
//        xiaoming.name = "小明";
//        xiaoming.age = 3;
//
//        System.out.println(xiaoming.name);
//        System.out.println(xiaoming.age);
//
//        xh.name = "小红";
//        xh.age = 3;
//        System.out.println(xh.name);
//        System.out.println(xh.age);


        //实例化了一个对象
        //Person person = new Person();

        //System.out.println(person.name);//null
        Pet dog = new Pet();
        dog.name = "旺财";
        dog.age = 3;
        dog.shout();

        System.out.println(dog.name);
        System.out.println(dog.age);



    }
}
/*
多态的注意事项
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象! Fatherf1 = new Son();
不能重写的:
	1.static 方法属于类,它不属于实例
	2.final 常量;
	3.private 方法;
 */

instanceof判断

package com.lol.LiOu.oop;

import com.lol.LiOu.oop.Demo05.Person;
import com.lol.LiOu.oop.Demo05.Student;
import com.lol.LiOu.oop.Demo05.Teacher;

public class Application {

    public static void main(String[] args) {

        //Objict>Person>Student
        //Objict>Person>Teacher
        //Objict>String
    Object object = new Student();

        System.out.println(object instanceof  Student);
        System.out.println(object instanceof  Person);
        System.out.println(object instanceof  Object);
        System.out.println(object instanceof Teacher);
        System.out.println(object instanceof String);
        System.out.println("============================================");
        Person person = new Student();
        System.out.println(person instanceof  Student);
        System.out.println(person instanceof  Person);
        System.out.println(person instanceof  Object);
        System.out.println(person instanceof Teacher);
        //System.out.println(person instanceof String);编译报错
        Student student = new Student();
        System.out.println("============================================");
        System.out.println(student instanceof  Student);
        System.out.println(student instanceof  Person);
        System.out.println(student instanceof  Object);
        //System.out.println(student instanceof Teacher);编译报错
        //System.out.println(student instanceof String);编译报错


    }
}
答案:true
true
true
false
false
============================================
true
true
true
false
============================================
true
true
true

类型转换

/*
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型
3.把父类转换为子类,向下转型;强制转换
4.方便方法的调用,减少重复的代码
*/
public class Demo01 {
    {//2.赋初始值
        System.out.println("匿名代码块");
    }
    static {//1.     只执行一次
        System.out.println("静态代码块");
    }
    public Demo01() {//3.
        System.out.println("构造方法");

    }

    public static void main(String[] args) {
        Demo01 demo011 = new Demo01();
        System.out.println("==============================");
        Demo01 demo012 = new Demo01();
    }
}
package com.lol.LiOu.oop.Demo05;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Demo02 {
    public static void main(String[] args) {
        System.out.println(random());
        System.out.println(PI);
    }
}

抽象类

package com.lol.LiOu.oop.Demo06;

//抽象类的所有方法继承他的子类 ,都必须要实现它的方法
public class A extends Action{
    @Override
    public void doSomething() {

    }
}

package com.lol.LiOu.oop.Demo06;

//abstract 抽象类:类 extends 单继承 (接口可以多继承)
public abstract class Action {

    //abstract:抽象方法,只有方法的名字,没有方法的实现!
    public abstract void doSomething();


    //1.不能new出来,只能考子类约束出来
    //2.抽象类中能写普通方法,抽象方法必须写在抽象类中
    //3.抽象的抽象是约束
    //存在意义  抽象出来 提高开发效率
}

接口的作用

1.约束

2.定义一些方法,让不同的人实现,很多人实现一个接口

3.public abstr

4.public static final

5.接口不能被实例化,接口中没有构造方法

6.implements可以实现多个接口

7.必须要重写接口中的方法

接口的定义和实现

package com.lol.LiOu.oop.Demo07;

//interface 定义关键字
public interface UserService {
    //public abstract是默认的public abstract void run();可以转换为void run();
    //接口中的所有定义其实都是抽象来的
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);

}

package com.lol.LiOu.oop.Demo07;

public class UserServicelmpl implements UserService,TimeService{
    @Override
    public void timer() {

    }

    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }
}

package com.lol.LiOu.oop.Demo07;

public interface TimeService {
    void timer();
}

一个java类中可以有很多class类,但是只能有一个public类

package com.lol.LiOu.oop.Demo08;

public class Outer {
    private int id=10;
    public void out(){
        System.out.println("这是外面类的方法");
    }
    public class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
        //
        public void getID(){
            System.out.println(id);
        }
    }
}
运行代码
package com.lol.LiOu.oop;

import com.lol.LiOu.oop.Demo08.Outer;

public class Application {
    public static void main(String[] args) {
      //外部类
        Outer outer = new Outer();

        //内部类通过外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.getID();
    }
}
posted @ 2021-01-13 17:31  JAVA初当力  阅读(47)  评论(0编辑  收藏  举报