一 抽象类的匿名对象
抽象类的匿名对象一般指没有被引用类型保存的对象,用完一次就会被回收。
public abstract class Shape {
public abstract void draw();
}
public class Rect extends Shape{
public void draw(){
System.out.println("画一个矩形");
}
}
public class Cycle extends Shape{
public void draw(){
System.out.println("画一个圆");
}
}
public class Test {
public static void drawMap(Shape shape){
shape.draw();
}
public static void main(String[] args) {
/* Shape shape=new Cycle();
Shape shape1=new Rect();*/
Cycle cycle=new Cycle();
Rect rect=new Rect();
drawMap(rect);
drawMap(cycle);
rect.draw();
cycle.draw();
drawMap(new Rect());//匿名调用
drawMap(new Cycle());
new Rect().draw();
new Cycle().draw();
}
}
匿名对象一般用于以下场景:
- 临时用途的对象:对象只在某一特定方法中被使用一次,创建一个引用没有意义。
- 简化代码:避免声明和管理不必要的变量,减少代码冗余。
- 回调和监听器:通常在实现接口或抽象类的一些回调方法时会使用这种方式。
二 接口
1.接口的概念
接口就是公共的行为规范,大家在实现时,只要符合标准,就可以使用,是一种引用数据类型。
2.接口的语法规则
将class关键字换成了interface
public interface 接口名称{}
public interface IShape {
void draw();
}
public class Cycle implements IShape{
public void draw(){
System.out.println("画一个圆");
}
}
public class Rect implements IShape{
public void draw(){
System.out.println("画一个矩形");
}
}
public class Test {
public static void drawMap(IShape iShape){
iShape.draw();
}
public static void main(String[] args) {
IShape iShape=new Rect();
IShape iShape1=new Cycle();
iShape.draw();
iShape1.draw();
drawMap(iShape);
drawMap(iShape1);
}
}
注:
1.接口里面都是抽象方法,在修饰抽象方法public abstract可以不写,推荐的写法void + 方法名字 + ();
2.在创建接口时,应该用大写I开头,使用形容词性的单词。
3.接口的使用
public class 类名称 implements 接口名称{}
类和接口之间的关系implements的关系
public interface USB {
void openDevice();
void closeDevice();
}
public class KeyBoard implements USB{
public void openDevice(){
System.out.println("打开键盘");
}
public void closeDevice(){
System.out.println("关闭键盘");
}
public void print(){
System.out.println("输入");
}
}
public class Mouse implements USB{
public void openDevice(){
System.out.println("打开鼠标");
}
public void closeDevice(){
System.out.println("关闭鼠标");
}
public void point(){
System.out.println("点击");
}
}
public class Computer{
public void open(){
System.out.println("开机");
}
public void close(){
System.out.println("关机");
}
public void useDevice(USB usb){
usb.openDevice();
if(usb instanceof Mouse){
Mouse mouse=(Mouse) usb;
mouse.point();
}else {
KeyBoard keyBoard=(KeyBoard) usb;
keyBoard.print();
}
usb.closeDevice();
}
}
public class Test {
public static void main(String[] args) {
Computer computer=new Computer();
computer.open();
computer.useDevice(new KeyBoard());
computer.useDevice(new Mouse());
computer.close();
}
}
4.接口的特性
4.1 接口是一种引用类型,但不能直接new接口的对象
public interface IA {
void write();
}
不然的话会直接报错。
4.2 接口中的每一个方法都是public abstract修饰,其他修饰都会报错。
public interface IA {
public abstract void write();
//private abstract void draw();
}
4.3 接口中的方法不能在接口中实现,只能在接口的类实现
public interface IA {
public abstract void write(){
System.out.println("写一本书");
}
public abstract void draw(){
System.out.println("画一幅画");
}
}
代码会直接报错。
4.4 从写接口的方法时,不能使用默认权限。
public class Test implements IA{
void draw(){}
void write(){}
会直接报错。
4.5 接口中不能有静态代码块和实例代码块
4.6 接口后面的字节码文件后缀格式和类一样都是 .class文件。
4.7 如果一个类没有重写接口所有的抽象方法,那么这个类一定是抽象类。
5.实现多个接口
一个类也可以实现多个接口。
public interface IFlying {
void fly();
}
public interface IRunning {
void run();
}
public interface ISwimming {
void swim();
}
public class Animal {
public String name;
public int age;
public Animal(String name,int age){
this.name=name;
this.age=age;
}
public void eat(){
System.out.println(this.name+"正在吃");
}
}
public class Dog extends Animal implements ISwimming,IRunning{
public Dog(String name,int age){
super(name,age);
}
public void swim(){
System.out.println(this.name+"正在游泳");
}
public void run(){
System.out.println(this.name+"正在跑");
}
public void eat(){
System.out.println(this.name+"正在吃狗粮");
}
}
public class Bird extends Animal implements IFlying{
public Bird(String name, int age) {
super(name, age);
}
@Override
public void fly() {
System.out.println(this.name+"正在飞");
}
public void eat(){
System.out.println(this.name+"正在吃鸟粮");
}
}
public class Fish extends Animal implements ISwimming{
public Fish(String name, int age) {
super(name, age);
}
@Override
public void swim() {
System.out.println(this.name+"正在游泳");
}
public void eat(){
System.out.println(this.name+"正在吃鱼食");
}
}
public class Test {
public static void swim(ISwimming iSwimming){
iSwimming.swim();
}
public static void run(IRunning iRunning){
iRunning.run();
}
public static void fly(IFlying iFlying){
iFlying.fly();
}
public static void main(String[] args) {
fly(new Bird("大鸟",12));
System.out.println("---------");
swim(new Fish("小于",12));
swim(new Dog("旺财",12));
System.out.println("---------");
run(new Dog("旺财",12));
}
public static void eatFood(Animal animal){
animal.eat();
}
public static void main1(String[] args) {
eatFood(new Dog("旺财",12));
eatFood(new Fish("小于",12));
eatFood(new Bird("大鸟",12));
}
}
创建三个接口 跑,飞,游泳,创建几个动物 狗,鸟,鱼然后分别与特性进行匹配。
体现了一个另类可以实现多个接口。
注:有了接口之后,程序员不用关注是哪种动物,只要会跑,满足这种特性就行
public class Robot implements IRunning {
@Override
public void run() {
System.out.println("机器人会跑");
}
}
6.接口与接口的继承
在Java中接口和接口之间也可以继承,用extends关键词,其作用就是扩大接口的功能。
interface IB{
void b();
}
interface IC{
void c();
}
interface ID extends IB,IC{
void d();
}
class m implements ID{
@Override
public void b() {
System.out.println("b");
}
@Override
public void c() {
System.out.println("c");
}
@Override
public void d() {
System.out.println("d");
}
}
三 Object类
Object是所有类的父类,默认会继承Object,所有类的对象都可以使用Object类的引用接收。
1.Object类中的重要方法
1.1 toString() 方法
@Override
public String toString() {
return "类的自定义字符串表示形式";
}
1.2 equals(Object obj)
public class Pig extends Animal{
public Pig(String name, int age) {
super(name, age);
}
public boolean equals(Object obj){
if(obj==null){
return false;
}
if (this==obj){
return true;
}
if(!(obj instanceof Pig)){
return false;
}
Pig pig=(Pig) obj;
return pig.equals(this.name) &&pig.age==this.age;
}
}
public static void main(String[] args) {
Pig pig=new Pig("zhu",1);
Pig pig1=new Pig("ba",3);
System.out.println(pig==pig1);
System.out.println(pig.equals(pig1));
}
1.3 hashCode()
@Override
public int hashCode() {
return Objects.hash(name, age);
}
希望能对大家有所帮助!!!!