`
beefcow
  • 浏览: 44511 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

{Java Detail} Interface and Abstract Class

阅读更多

关于接口,一个一直不确定的问题就是它到底能不能拥有字段,下面的代码回答:可以。

 

由此引出有意思的一点是,java 中,接口的字段居然是默认的会以 public final static 修饰,何以如此?见代码注释。

 

  • 其实最精髓的就是把握Java中接口的设计原则:它更像一种契约,白纸黑字,无可更改。
  • 至于抽象类,它和普通类的唯一本质区别,在我看来,就是它可以包含无方法体的抽象方法,而由此,也导致它不能直接实例化。
  • 至于两者的区别,上面谈到的字段算一个,其实最本质的我感觉还是设计理念不同,一个很好的说法是 接口 是用来代表Can Do 的,而 抽象类 则是 is a.    超人和鸭子都Can fly,但两者显然非同一物种。所以好的做法是 两者都实现Fly 接口,而鸭子,还是和鸡啊鹅啊什么的去一起继承自 禽 这一抽象类吧。

 

 

  • 接口
/** 
* 1.can interface have fields? yes,but it is [public final and static] by 
* default. 
* 
* Interface is like an contract,class who have used it must follow it. so it is 
* important that the contract is quite stable(or immutable), 
* 
* so the fields is final(can not be changed) and static(every one 
* has the same copy of it,no one is different). 
* 
* while,it is also public(if it is private,what does it used for? It is 
* useless). 
* 
* @author mmLiu 
* 
*/ 
public interface myInterface { 
public final static int filed1 = 1; 
} 

 

  •  抽象类
/**
 * I think the only essential difference between abstract class and normal class
 * is that the abstract one can have unimplemented method(so called abstract
 * method).
 * <p>
 * and As a result,it can not be initialized.
 * 
 * @author mmLiu
 * 
 */
public abstract class myAbstractClass {
	int myInt;

	/**
	 * Constructor
	 */
	public myAbstractClass() {
		System.out.println("Parent's Constructor");
	}

	/**
	 * Abstract method does not have method body(otherwise how can it be called
	 * abstract?).
	 * <p>
	 * while ,an abstract class does not have to have an abstract method.you can
	 * safely remove this method,it is still right.
	 * 
	 * @param a
	 * @return
	 */
	public abstract int myVirtualMethod(int a);

	public int myMethod2(int a) {
		return a;
	}
}

 

 

 

ps.关于这种文章主体存在于代码中的写作方式,我承认是因为我懒,我自己都很怀疑这样的文章,除对我存在可读性外,是否还能有他人,但考虑到我的博客会不会真的居然还会有读者,所以,让我懒下去吧。

 

 

 

1
1
分享到:
评论

相关推荐

    C Interface and Implement

    C Interfaces and Implementations shows how to create reusable APIs using interface-based design, a language-independent methodology that separates interfaces from their implementations. This ...

    Java_for_the_Web_with_Servlets

    - **`GenericServlet` Wrapper Class**: This abstract class provides a convenient implementation of the `Servlet` interface and can be extended to create custom servlets. - **Thread-Safe Servlets**: ...

    Jpaca 帮助文档 Jpcap API EN.CHM

    Each class, interface, nested class and nested interface has its own separate page. Each of these pages has three sections consisting of a class/interface description, summary tables, and detailed ...

    PIM_接口_PIM_assignment_java上机实验_pimcmd_

    The assignment requires that you create a class for each item type and that each class extends an abstract base class provided for you. 文件列表:PIMAppointment.javaPIMContact.javaPIMEntity....

    Java.EE.7.Performance.Tuning.and.Optimization.178217642X

    Title: Java EE 7 Performance Tuning and Optimization Author: Osama Oransa Length: 398 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2014-06-25 ISBN-10: 178217642X ...

    Java.EE.Development.with.Eclipse.2nd.Edition.178528534

    Use JSP, Servlet, JSF, and EJBs to create a user interface and write business logic Create JEE database applications using JDBC and JPA Handle asynchronous messages using MDBs for better scalability ...

    Learning.Reactive.Programming.With.Java.8

    Learn about Java 8's lambdas and what reactive programming is all about, and how these aspects are utilized by RxJava Build fast and concurrent applications with ease, without the complexity of Java's...

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    Chapter 4Interfacesdescribes how to declare interface types that are abstract descriptions of behavior that provide maximum flexibility for class designers and implementors. Chapter 5Nested Classes ...

    Java.Persistence.with.Hibernate.2nd.Edition

    collections, and complex class associations. Finally, we discuss integration with legacy database schemas and some mapping strategies that are especially tricky. Part 3, “Transactional data ...

    Java自定义异常案例--ExceptionManager(java源码)

    * This class &lt;code&gt;ExceptionManager&lt;/code&gt; and its subclasses are a form of * &lt;code&gt;Exception&lt;/code&gt;. It is used to wrap all the &lt;code&gt;Throwable&lt;/code&gt; instances * and handle them in a unified way...

    Java Performance Companion(Addison,2016)

    Picking up where Charlie Hunt and Binu John’s classic Java Performance left off, this book provides unprecedented detail on two powerful Java platform innovations: the Garbage First (G1) garbage ...

    Exploring java 9 [pdf]

    Java SE 9 was released on September 21, 2017. It’s the first major release of the Java platform since Java SE 8 was released on March 18, 2014. The Java community has been ...to Java SE 9 and JDK 9.

    TCP-IP Sockets in Java. Practical Guide for Programmers

    The API (application programming interface) reference sections in each chapter, which describe the relevant parts of each class, have been replaced with (i) a summary section that lists the classes ...

    JAVA贪吃蛇源代码

    1. **基本数据结构**:游戏中的蛇、食物和边界可以用类(Class)来表示。例如,`Snake`类包含蛇的身体部分(body),方向(direction)和长度(length);`Food`类用于表示随机生成的食物位置;`Boundary`类则定义了...

Global site tag (gtag.js) - Google Analytics