09day
Object object = new Student();
System.out.println(object instanceof Student);//ture
System.out.println(object instanceof Person);//ture
System.out.println(object instanceof Object);//ture
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
System.out.println("==========");
Person person = new Student();
System.out.println(person instanceof Student);//ture
System.out.println(person instanceof Person);//ture
System.out.println(person instanceof Object);//ture
System.out.println(person instanceof Teacher);//false
//System.out.println(person instanceof String);//编译出错
System.out.println("==========");
Student student = new Student();
System.out.println(student instanceof Student);//ture
System.out.println(student instanceof Person);//ture
System.out.println(student instanceof Object);//ture
//System.out.println(student instanceof Teacher);//编译出错
//System.out.println(student instanceof String);//编译出错
类型转换
-
父类引用指向子类的对象
-
把子类转换为父类,向上转型
-
把父类转换成子类,向下转型;强制转换(可能会丢失精度)
-
方便方法的调用,减少重复代码
public static void main(String[] args) {
//类型之间的转换:基本类型转换 父->子
//高 //低 低转高
Person student = new Student();
//将Person类型对象student转换为Student类型,我们就可以使用Student类型的方法啦
//子类转换为父类,可能会丢失一些方法
((Student)student).go();
Student student1 = new Student();
Person person=student;
}
static
public class Student {
private static int age;//静态变量 多线程
private double score;//非静态变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);
//System.out.println(Student.score);//非静态的变量不可以通过类名调用
System.out.println(s1.age);
System.out.println(s1.score);
System.out.println("=========");
//Student student = new Student();
//student.run();
Student.go();//静态的可以直接被调用 go();
}
}
public class Person {
//2.第二输出 赋初始值
{
//匿名代码块
System.out.println("匿名代码块");
}
//1.先输出 但只执行一次
static {
//静态代码块
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();
}
}
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);//随机数
}
}
抽象类
abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么该类就是抽象类
-
抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明抽象类
-
抽象类不能使用new关键字来创建对象,他是用来让子类继承的
-
抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的
-
子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则该类也要声明为抽象类
//A
public class A extends Action{
public void doSomeThing() {
}
}
//Action
public abstract class Action {
//约束~ 有人帮我们实现
//抽象方法,只有方法的名字,没有方法的实现
public abstract void doSomeThing();
//不能new出来,只能靠子类实现它
}
接口
普通类:只有具体实现
抽象类:具体实现和规范(抽象方法)都有
接口:只有规范,自己无法写方法(专业的约束)约束和实现分离:面向接口分离
接口的本质是契约
关键字:interface
作用:
-
约束
-
定义一些方法,让不同的人实现
-
方法默认都是:public abstract
-
常量默认都是:public static final
-
接口不能被实例化,接口中没有构造方法
-
接口可以实现多个implements
-
必须要重写接口中的方法
内部类
内部类就是在一个类的内部再定义一个类
-
成员内部类
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);
}
}
}public static void main(String[] args) { //new 外部类 Outer outer = new Outer(); //通过外部类实例化内部类 Outer.Inner inner = outer.new Inner(); inner.getID(); } -
静态内部类
public class Outer { private int id=10; public void out(){ System.out.println("这是外部类的方法"); } public static class Inner{ public void in(){ System.out.println("这是内部类的方法"); } //获得外部类的私有属性 } } -
局部内部类
//局部类 class A{ public static void main(String[] args) { } } //局部内部类 public void method(){ class A{ public void in(){ } } } -
匿名内部类
public class Test { public static void main(String[] args) { //没有名字初始化类,不用将实例保存在变量中 new Apple().eat(); //匿名类 UserService userService = new UserService(){ @Override public void hello() { } }; } } class Apple{ public void eat(){ System.out.println("1"); } } interface UserService{ void hello(); }
# 09day
## instanceof
```javaObject object = new Student(); System.out.println(object instanceof Student);//ture System.out.println(object instanceof Person);//ture System.out.println(object instanceof Object);//ture System.out.println(object instanceof Teacher);//false System.out.println(object instanceof String);//false
System.out.println("==========");
Person person = new Student(); System.out.println(person instanceof Student);//ture System.out.println(person instanceof Person);//ture System.out.println(person instanceof Object);//ture System.out.println(person instanceof Teacher);//false //System.out.println(person instanceof String);//编译出错
System.out.println("=========="); Student student = new Student(); System.out.println(student instanceof Student);//ture System.out.println(student instanceof Person);//ture System.out.println(student instanceof Object);//ture //System.out.println(student instanceof Teacher);//编译出错 //System.out.println(student instanceof String);//编译出错```
## 类型转换
1. 父类引用指向子类的对象2. 把子类转换为父类,向上转型3. 把父类转换成子类,向下转型;强制转换(可能会丢失精度)4. 方便方法的调用,减少重复代码
```javapublic static void main(String[] args) { //类型之间的转换:基本类型转换 父->子 //高 //低 低转高 Person student = new Student(); //将Person类型对象student转换为Student类型,我们就可以使用Student类型的方法啦 //子类转换为父类,可能会丢失一些方法 ((Student)student).go(); Student student1 = new Student(); Person person=student; }```
## static
```javapublic class Student { private static int age;//静态变量 多线程 private double score;//非静态变量 public void run(){ } public static void go(){ } public static void main(String[] args) { Student s1 = new Student(); System.out.println(Student.age); //System.out.println(Student.score);//非静态的变量不可以通过类名调用 System.out.println(s1.age); System.out.println(s1.score); System.out.println("========="); //Student student = new Student(); //student.run(); Student.go();//静态的可以直接被调用 go(); }}```
```javapublic class Person { //2.第二输出 赋初始值 { //匿名代码块 System.out.println("匿名代码块"); } //1.先输出 但只执行一次 static { //静态代码块 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(); }}```
```javaimport 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);//随机数 }}```
## 抽象类
***abstract***修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么该类就是抽象类
- 抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明抽象类- 抽象类不能使用new关键字来创建对象,他是用来让子类继承的- 抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的- 子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则该类也要声明为抽象类
```java//Apublic class A extends Action{ @Override public void doSomeThing() {
}}```
```java//Actionpublic abstract class Action { //约束~ 有人帮我们实现 //抽象方法,只有方法的名字,没有方法的实现 public abstract void doSomeThing(); //不能new出来,只能靠子类实现它}```
## 接口
普通类:只有具体实现
抽象类:具体实现和规范(抽象方法)都有
接口:只有规范,自己无法写方法(专业的约束)约束和实现分离:面向接口分离
***接口的本质是契约***
关键字:**interface**
作用:
1. 约束2. 定义一些方法,让不同的人实现3. 方法默认都是:public abstract4. 常量默认都是:public static final5. 接口不能被实例化,接口中没有构造方法6. 接口可以实现多个implements7. 必须要重写接口中的方法
## 内部类
内部类就是在一个类的内部再定义一个类
1. 成员内部类
```java 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); } } } ```
```java public static void main(String[] args) { //new 外部类 Outer outer = new Outer(); //通过外部类实例化内部类 Outer.Inner inner = outer.new Inner(); inner.getID(); } ```
2. 静态内部类
```java public class Outer { private int id=10; public void out(){ System.out.println("这是外部类的方法"); } public static class Inner{ public void in(){ System.out.println("这是内部类的方法"); } //获得外部类的私有属性 } } ```
3. 局部内部类
```java //局部类 class A{ public static void main(String[] args) { } } //局部内部类 public void method(){ class A{ public void in(){ } } } ```
4. 匿名内部类
```java public class Test { public static void main(String[] args) { //没有名字初始化类,不用将实例保存在变量中 new Apple().eat(); //匿名类 UserService userService = new UserService(){ @Override public void hello() { } }; } } class Apple{ public void eat(){ System.out.println("1"); } } interface UserService{ void hello(); } ```

浙公网安备 33010602011771号