当前位置:网站首页>抽象类与抽象方法
抽象类与抽象方法
2022-07-06 10:47:00 【青年要奋斗】
抽象类与抽象方法
在java中,抽象类不能产生对象实例。定义抽象时,需要使用abstract关键字,语法:
[权限修饰符] abstract class 类名 {
类体
}
使用abstract定义的类被称为抽象类,使用abstract定义的方法被称为抽象方法,语法:
[权限修饰符] abstract 方法返回值类型 方法名(参数列表);
实例:使用抽象类模拟“去商场买衣服”场景
定义Market类
public abstract class Market {
public String name; // 商场名称
public String goods; // 商品名称
public abstract void shop(); // 抽象方法,用来输出信息
}
定义TaobaoMarket类
public class TaobaoMarket extends Market {
public void shop() {
System.out.println(name + "网购" + goods);
}
}
定义WallMarket类
public class WallMarket extends Market {
public void shop() {
System.out.println(name + "实体店购买" + goods);
}
}
定义GoShopping类
public class GoShopping {
public static void main(String[] args) {
Market market = new WallMarket(); // 使用派生类对象创建抽象类对象
market.name = "沃尔玛";
market.goods = "七匹狼西服";
market.shop();
market = new TaobaoMarket(); // 使用派生类对象创建抽象类对象
market.name = "淘宝";
market.goods = "韩都衣舍花裙";
market.shop();
}
}
运行结果如下:
沃尔玛实体店购买七匹狼西服
淘宝网购韩都衣舍花裙
Process finished with exit code 0
父类对象new子类对象
Animal b = new Dog();
如上,对象b调用属性的时候调用父类属性,将对象b调用方法的时候调用子类的方法。
class Animal{
}
class Dog extends Animal{
}
public class Test4 {
public static void main(String[] args) {
Animal a = new Animal();
Animal b = new Dog();
System.out.println(a instanceof Animal);
System.out.println(a.getClass());
System.out.println(b.getClass());
//先用的子类的方法所以调用的是子类的getClass
System.out.println(a.getClass() .equals(b.getClass()));
}
}
Dog b = new Dog();
Animal b = new Dog();
结果一样的,但是有区别的,看看使用抽象类模拟“去商场买衣服”场景,如果将Market market = new WallMarket();
改成WallMarket market = new WallMarket();
,编译器会报错,记住:此父类有两个子类。
再举个例子
定义一个demo1类:
package Demo;
public class demo1 {
public static void main(String[] args) {
Teacher Li = new English();
Teacher Yang = new Math();
Li.teaching();
Yang.teaching();
}
}
定义一个Teacher类
package Demo;
public abstract class Teacher{
//抽象类
abstract public void teaching(); //抽象方法
}
class Math extends Teacher{
@Override
public void teaching() {
System.out.println("我们讲三角函数!");
}
}
class English extends Teacher{
@Override
public void teaching() {
System.out.println("Good morning class!");
}
}
运行结果如下:
Good morning class!
我们讲三角函数!
Process finished with exit code 0
注意:抽象类是不能实例化的!
Teacher Li = new Teacher(); //错误的写法!
报错的结果如下
java: Demo.Teacher是抽象的; 无法实例化
知道抽象类有什么用的!
边栏推荐
- 使用block实现两个页面之间的传统价值观
- Method of accessing mobile phone storage location permission under non root condition
- Epoll () whether it involves wait queue analysis
- 十、进程管理
- Windows connects redis installed on Linux
- AFNetworking框架_上传文件或图像server
- [.Net core] solution to error reporting due to too long request length
- Stm32+esp8266+mqtt protocol connects onenet IOT platform
- 首先看K一个难看的数字
- 使用cpolar建立一个商业网站(1)
猜你喜欢
Summary of performance knowledge points
From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
Numerical analysis: least squares and ridge regression (pytoch Implementation)
Medical image segmentation
线代笔记....
There is a sound prompt when inserting a USB flash disk under win10 system, but the drive letter is not displayed
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
Virtual machine VirtualBox and vagrant installation
30 minutes to understand PCA principal component analysis
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
随机推荐
From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
上海部分招工市场对新冠阳性康复者拒绝招录
Maixll dock camera usage
UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
Blue Bridge Cup real question: one question with clear code, master three codes
MySQL查询请求的执行过程——底层原理
Ms-tct: INRIA & SBU proposed a multi-scale time transformer for motion detection. The effect is SOTA! Open source! (CVPR2022)...
Distiller les connaissances du modèle interactif! L'Université de technologie de Chine & meituan propose Virt, qui a à la fois l'efficacité du modèle à deux tours et la performance du modèle interacti
A method of sequentially loading Unity Resources
图片缩放中心
Afnetworking framework_ Upload file or image server
Brief description of SQL optimization problems
Splay
随着MapReduce job实现去加重,多种输出文件夹
Docker installation redis
POJ 2208 six lengths of tetrahedron are known, and the volume is calculated
SQL injection - access injection, access offset injection
[swoole series 2.1] run the swoole first
Describe the process of key exchange
Penetration test information collection - WAF identification