Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java abstract classes and interfaces

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

本篇内容主要讲解"Java抽象类和接口怎么使用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java抽象类和接口怎么使用"吧!

看一下具体的例子,有一些小细节平时可能不会注意。

class A {}interface M extends N,L{//正确的,不会报错}interface N{}interface L{}interface 接口 { public final int i = 1;//变量默认都为public final修饰 final A a = null;//基本数据类型和引用都一样 //protected void a();//报错 //private //报错 public abstract void a();// 方法都是public abstract修饰的。 //void b(){} 报错,接口里的方法不能有方法体,也不能有{},只能有(); // final void b(); // 注意,抽象方法不能加final。因为final方法不能被重写。 //但如果抽象方法不被重写那就没有意义了,因为他根本没有代码体。}abstract class 抽象类 { public final int i = 1;//变量并没有被pulic和final修饰,只是一般的成员变量 public final A a = null; private void A(){}//抽象类可以有具体方法 abstract void AA();//抽象方法没有方法体 //private abstract void B();//报错,组合非法 // 因为private修饰的方法无法被子类重写,所以和final一样,使抽象方法无法被实现。}//抽象类也可以被实例化,举例说明abstract class B{ B() { System.out.println("b init"); }}class C extends B{ C(){ super(); System.out.println("c init"); }}public class 接口对比抽象类 { @Test public void test() { C c = new C(); //结果先实例化B,再实例化C。 //因为会调用到父类的构造方法。 }}到此,相信大家对"Java抽象类和接口怎么使用"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report