目录
抽象类通过子类向上转型完成实例化(子类需重写抽象类的方法,否则添加abstract;一个子类只能继承一个抽象类)
执行顺序:super();-->指向父类的构造方法。先执行父类的构造方法,在执行子类的构造方法。
final修饰的是常量,不能被修改;抽象类中方法子类继承后需要被重写。
5.1 接口存储的属性一定是常量,默认为 public static final
1.抽象类概念及定义
在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的。如果一个类中没有包含足够的信息来描绘一个对象(类的方法中没有方法体,必须添加abstract作修饰),这样类就是抽象类。
public abstract class animal {//定义一个抽象类
//抽象方法--没有方法体,用abstract作修饰
public abstract void move() ;
public abstract void eat();
}
2.抽象类的使用
2.1 实例化
抽象类不能进行实例化
--Cannot instantiate the type animal
抽象类通过子类向上转型完成实例化(子类需重写抽象类的方法,否则添加abstract;一个子类只能继承一个抽象类)
public class AnimalTest {
@Test
public void test() {
animal anemonefish = new Fish();//小丑鱼
anemonefish.move();
anemonefish.eat();
animal sparrow = new Bird();//麻雀
sparrow.move();
sparrow.eat();
}
}
public class Fish extends animal{
@Override
public void move() {
System.out.println("鱼能在水里游.");
}
@Override
public void eat() {
System.out.println("鱼吃小虾米.");
}
}
public class Bird extends animal{
@Override
public void move() {
System.out.println("鸟在天上飞.");
}
@Override
public void eat() {
System.out.println("鸟吃粮食.");
}
}
执行结果如下图所示:
2.2 构造方法--存在(实现初始化)
执行顺序:super();-->指向父类的构造方法。先执行父类的构造方法,在执行子类的构造方法。
public abstract class animal {
private String color;
private String category;//类别
public animal(String color, String category) {
super();
this.color = color;
this.category = category;
}
public abstract void move() ;
public abstract void eat();
}
2.3 使用final去修饰抽象类--不可以
final修饰的是常量,不能被修改;抽象类中方法子类继承后需要被重写。
3.抽象类应用实例--简易计算器[9以内的加减乘除]
package AbstractExample;
/**
* 计算器的抽象类
* @author 皮卡丘
*/
public abstract class Caculator {
public abstract double add(double a,double b);
public abstract double sub(double a,double b);
public abstract double mul(double a,double b);
public abstract double divi(double a,double b);
// public Caculator() {
//
// }
public String run(String opt,double a,double b) {
String result = "0";
switch (opt) {
case "+":
result = add(a, b)+"";
break;
case "-":
result = sub(a, b)+"";
break;
case "*":
result = mul(a, b)+"";
break;
case "÷":
if(b == 0) {
result = "ERROR";
}else {
result = divi(a, b)+"";
}
break;
default:
break;
}
return result;
}
}
package AbstractExample.impl;
/**
* 计算器方法的实现
*/
import AbstractExample.Caculator;
public class CaculatorImpl extends Caculator {
@Override
public double add(double a, double b) {
return a+b;
}
@Override
public double sub(double a, double b) {
return a-b;
}
@Override
public double mul(double a, double b) {
return a*b;
}
@Override
public double divi(double a, double b) {
return a/b;
}
}
package AbstractExample.view;
/**
* 简易计算器视图
*/
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import AbstractExample.Caculator;
import AbstractExample.impl.CaculatorImpl;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CaculatorView extends JFrame {
private JTextField outputField;
private JButton Btn_0;
private JButton Btn_1;
private JButton Btn_2;
private JButton Btn_3;
private JButton Btn_4;
private JButton Btn_5;
private JButton Btn_6;
private JButton Btn_7;
private JButton Btn_8;
private JButton Btn_9;
private JButton equalBtn;
private JButton addBtn;
private JButton subBtn;
private JButton mulBtn;
private JButton diviBtn;
private StringBuilder sb;
private Caculator ac;
private String first;//记录输入的值
private String second = "0";
private String opt = "0";//记录运算符号
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CaculatorView frame = new CaculatorView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CaculatorView() {
sb = new StringBuilder();
ac = new CaculatorImpl();
getContentPane().setForeground(Color.DARK_GRAY);
getContentPane().setFont(new Font("宋体", Font.PLAIN, 35));
setTitle("简易计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 454, 490);
getContentPane().setLayout(null);
outputField = new JTextField();
outputField.setHorizontalAlignment(SwingConstants.RIGHT);
outputField.setFont(new Font("Arial", Font.BOLD, 60));
outputField.setText("0");
outputField.setBounds(0, 0, 432, 126);
getContentPane().add(outputField);
outputField.setColumns(10);
Btn_7 = new JButton("7");
Btn_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "7";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_7.setForeground(Color.DARK_GRAY);
Btn_7.setFont(new Font("Arial", Font.BOLD, 40));
Btn_7.setBounds(10, 141, 94, 55);
getContentPane().add(Btn_7);
Btn_8 = new JButton("8");
Btn_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "8";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_8.setForeground(Color.DARK_GRAY);
Btn_8.setFont(new Font("Arial", Font.BOLD, 40));
Btn_8.setBounds(129, 141, 94, 55);
getContentPane().add(Btn_8);
Btn_9 = new JButton("9");
Btn_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "9";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_9.setForeground(Color.DARK_GRAY);
Btn_9.setFont(new Font("Arial", Font.BOLD, 40));
Btn_9.setBounds(246, 141, 94, 55);
getContentPane().add(Btn_9);
Btn_4 = new JButton("4");
Btn_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "4";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_4.setForeground(Color.DARK_GRAY);
Btn_4.setFont(new Font("Arial", Font.BOLD, 40));
Btn_4.setBounds(10, 208, 94, 55);
getContentPane().add(Btn_4);
Btn_5 = new JButton("5");
Btn_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "5";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_5.setForeground(Color.DARK_GRAY);
Btn_5.setFont(new Font("Arial", Font.BOLD, 40));
Btn_5.setBounds(129, 208, 94, 55);
getContentPane().add(Btn_5);
Btn_6 = new JButton("6");
Btn_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "6";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_6.setForeground(Color.DARK_GRAY);
Btn_6.setFont(new Font("Arial", Font.BOLD, 40));
Btn_6.setBounds(246, 211, 94, 55);
getContentPane().add(Btn_6);
Btn_1 = new JButton("1");
Btn_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "1";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_1.setForeground(Color.DARK_GRAY);
Btn_1.setFont(new Font("Arial", Font.BOLD, 40));
Btn_1.setBounds(10, 278, 94, 55);
getContentPane().add(Btn_1);
Btn_2 = new JButton("2");
Btn_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "2";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_2.setForeground(Color.DARK_GRAY);
Btn_2.setFont(new Font("Arial", Font.BOLD, 40));
Btn_2.setBounds(129, 278, 94, 55);
getContentPane().add(Btn_2);
Btn_3 = new JButton("3");
Btn_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "3";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_3.setForeground(Color.DARK_GRAY);
Btn_3.setFont(new Font("Arial", Font.BOLD, 40));
Btn_3.setBounds(246, 281, 94, 55);
getContentPane().add(Btn_3);
Btn_0 = new JButton("0");
Btn_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first = "0";
sb.append(first);
outputField.setText(sb.toString());
}
});
Btn_0.setForeground(Color.DARK_GRAY);
Btn_0.setFont(new Font("Arial", Font.BOLD, 40));
Btn_0.setBounds(10, 348, 94, 55);
getContentPane().add(Btn_0);
addBtn = new JButton("+");
addBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opt = "+";
sb.append(opt);
outputField.setText(sb.toString());
second = first;//把第一次输入的值赋给second
}
});
addBtn.setForeground(Color.DARK_GRAY);
addBtn.setFont(new Font("Arial", Font.BOLD, 40));
addBtn.setBounds(353, 141, 79, 55);
getContentPane().add(addBtn);
subBtn = new JButton("-");
subBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opt = "-";
sb.append(opt);
outputField.setText(sb.toString());
second = first;
}
});
subBtn.setForeground(Color.DARK_GRAY);
subBtn.setFont(new Font("Arial", Font.BOLD, 40));
subBtn.setBounds(355, 208, 79, 55);
getContentPane().add(subBtn);
mulBtn = new JButton("*");
mulBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opt = "*";
sb.append(opt);
outputField.setText(sb.toString());
second = first;
}
});
mulBtn.setForeground(Color.DARK_GRAY);
mulBtn.setFont(new Font("Arial", Font.BOLD, 40));
mulBtn.setBounds(353, 278, 79, 55);
getContentPane().add(mulBtn);
diviBtn = new JButton("÷");
diviBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opt = "÷";
sb.append(opt);
outputField.setText(sb.toString());
second = first;
}
});
diviBtn.setForeground(Color.DARK_GRAY);
diviBtn.setFont(new Font("楷体", Font.BOLD, 30));
diviBtn.setBounds(353, 348, 79, 55);
getContentPane().add(diviBtn);
equalBtn = new JButton("=");
equalBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double a = Double.parseDouble(second);
double b = Double.parseDouble(first);
String result = ac.run(opt,a,b);
outputField.setText(result);
//运算完成后进行清零操作
second = "0";
first = "0";
sb.delete(0,sb.length());//字符串清零
}
});
equalBtn.setForeground(Color.DARK_GRAY);
equalBtn.setFont(new Font("Arial", Font.BOLD, 40));
equalBtn.setBounds(129, 348, 209, 55);
getContentPane().add(equalBtn);
}
}
部分运行结果如下:
-->
-->
4.接口概念及定义
接口是功能的集合,只描述应具备的方法,并不实现功能,通过其实现类完成。可通过接口来支持多继承实现。
JDK版本1.8以前接口中只允许存在抽象方法,版本1.8以后允许使用default(接口中一定是抽象方法,所以可以省略public abstract)
public interface Ianimal {
public abstract void move();
default void play(){
System.out.println("两头鲸鱼正在海里嬉戏");
}
}
5.接口的使用
5.1 接口存储的属性一定是常量,默认为 public static final
Getter和Setter也不能使用
public interface Ianimal {
String color = "white";
}
5.2 接口不存在构造方法
构造方法作用是初始化,但接口中数据均为常量,所以使用static修饰。static关键字具体解析--(java--static关键字&对象的初始化顺序_诺言的博客-优快云博客).
5.3 接口可以继承多个接口
5.4 接口不能被实例化(new)
子类对象向上转型进行实例化
public class whale implements Ianimal,Ieat{
@Override
public void move() {
System.out.println("鲸鱼泳速可达每小时55公里");
}
@Override
public void eat() {
System.out.println("鲸鱼以软体动物、鱼类、浮游动物为食");
}
}
public class InterfaceTest {
@Test
public void test() {
Ianimal w = new whale();
w.move();
w.play();
}
}
6.接口应用实例--简易计算器(9以内的加减法)
|--修改Caculator.java
package ICaculator;
/**
* 计算器的抽象类
* @author 皮卡丘
*/
public interface Caculator {
double add(double a,double b);
double sub(double a,double b);
double mul(double a,double b);
double divi(double a,double b);
default String run(String opt,double a,double b) {
String result = "0";
switch (opt) {
case "+":
result = add(a, b)+"";
break;
case "-":
result = sub(a, b)+"";
break;
case "*":
result = mul(a, b)+"";
break;
case "÷":
if(b == 0) {
result = "ERROR";
}else {
result = divi(a, b)+"";
}
break;
default:
break;
}
return result;
}
}
部分运行结果如下:
-->
-->