Java随堂小记08
instanceof
instanceof 用来测试一个对象是否为一个类的实例
对象必须为引用类型,不能是基本类型
如果a能转换成右边的class类型,则返回true,否则返回false
package www.Dongyi.oop.Demo7;
public class Application {
public static void main(String[] args) {
//Object > Person > Teacher
//Object > Person > Student
//Object > 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);//编译错误
System.out.println("==========================");
Student student = new Student();
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);//编译错误
}
}
package www.Dongyi.oop.Demo7;
public class Person {
public void run(){
System.out.println("run");
}
}
package www.Dongyi.oop.Demo7;
public class Student extends Person{
}
package www.Dongyi.oop.Demo7;
public class Teacher extends Person{
}
强制转换
1.父类引用指向子类对象
2.子类转为父类:向上转型
3.父类转为子类:向下转型 父类无法调用子类独有的方法,需要强制转换
package www.Dongyi.oop.Demo7;
public class Demo7 {
public static void main(String[] args) {
//类型之间的转化: 父->子
Person obj = new Student();//父类无法调用子类独有的方法
((Student)obj).go();//强制转换
}
}
package www.Dongyi.oop.Demo7;
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
static
package www.Dongyi.oop.Demo8;
public class Student {
private static int age;
private double score;
public void run(){
go();//非静态可以直接调用静态方法
// 因为static是和Student类一起加载,Student创建出来static也存在
}
public static void go(){
//静态可以调用静态的方法,但不能调用非静态
//不能调用run();Student加载的时候还没有run方法
}
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);
go();//静态方法直接调用
s1.run();//非静态要用对象来调用
}
}
代码块、构造器、static
package www.Dongyi.oop.Demo8;
public class Person {
{
System.out.println("匿名代码块");
}
static {
System.out.println("静态代码块");
}
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
}
}
运行结果
小结:
- 匿名代码块:
在构造器之前就存在了,可以赋初值 - 静态代码块:
可以初始化一些东西,静态方法和类一起加载,永久只执行一次,且是最早执行的
补充
package www.Dongyi.oop.Demo8;
//静态导入包
import static java.lang.Math.*;//.*直接导入所有方法
//import static java.lang.Math.random;//导入方法
public class Test {
//静态导入包
public static void main(String[] args) {
System.out.println(Math.random());//导包前
System.out.println(random());//导包后
}
}
被final关键字修饰的类不能成为任何类的父类
抽象类
- 抽象类被继承的时候,子类必须重写抽象类中所有的抽象方法
- 抽象类不能被实例化对象,但可以实例化抽象类的子类,因为他的子类重写了抽象方法不再是抽象的。
- 抽象类可以写普通方法,但抽象方法一定写在抽象类
- 虽然抽象类不能new,但是存在构造器
package www.Dongyi.oop.Demo9;
public abstract class Action {
public abstract void doSomething();
public Action(){
System.out.println("抽象类构造器");
}
}
抽象类被继承的时候,子类必须重写抽象类中所有的抽象方法
package www.Dongyi.oop.Demo9;
public class Run extends Action{
@Override
public void doSomething() {
System.out.println("构造器重写");
}
public Run() {
super();
}
public static void main(String[] args) {
Run run = new Run();
run.doSomething();
System.out.println(run);//对象
}
}
接口
接口都需要有实现类
一般名字为 接口名+Impl
implements 实现接口
抽象类只能单继承,但接口可以实现多继承
package www.Dongyi.oop.Demo10;
public class UserServiceImpl implements UserService,TimeService{//多继承
@Override
public void add(int time) {
}
@Override
public void delete(int time) {
}
@Override
public void update(int time) {
}
@Override
public void search(int time) {
}
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void search(String name) {
}
}
package www.Dongyi.oop.Demo10;
public interface UserService {
//增删改查
void add(String name);
void delete(String name);
void update(String name);
void search(String name);
}
package www.Dongyi.oop.Demo10;
public interface TimeService {
void add(int time);
void delete(int time);
void update(int time);
void search(int time);
}
内部类
- 内部类就是在一个类的内部再定义一个类
1.成员内部类
package www.Dongyi.oop.Demo11;
public class Outer {
private int id = 8000;
public void on(){
System.out.println("这是外部类的方法");
}
public class Inter{
public void in(){
System.out.println("这是内部类的方法");
}
//可以获得外部类的私有属性
public void getOuterId(){
System.out.println(id);
}
}
}
package www.Dongyi.oop.Demo11;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
outer.on();
Outer.Inter inter = outer.new Inter();
inter.in();
inter.getOuterId();//8000
}
}
2.静态内部类
加了static 无法得到id,除非id也为static的
3.局部内部类:类似于局部变量
public class Outer {
//局部内部类
public void method(){
class Inner{
public void in(){
}
}
}
}
4.匿名内部类
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("Apple");
}
}
interface UserService{
void hello();
}