{"id":724,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java Interface\u5173\u952e\u5b57","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/724.html","title":{"rendered":"Java Interface\u5173\u952e\u5b57"},"content":{"rendered":"
\n
\u5b83\u7528\u4e8e\u5b9e\u73b0\u62bd\u8c61\u3002<\/span> <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/body>
\n \u901a\u8fc7\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u652f\u6301\u591a\u91cd\u7ee7\u627f\u7684\u529f\u80fd\u3002<\/span>
\n \u53ef\u7528\u4e8e\u5b9e\u73b0\u677e\u6563\u8026\u5408\u3002<\/span>
\n <\/p>\n
\u5982\u4f55\u58f0\u660e\u63a5\u53e3\uff1f<\/h2>\n
\u8bed\u6cd5: <\/h3>\n
interface <
interface_name>
{
\/\/ declare constant fields
\/\/ declare methods that abstract
\/\/ by default.}
<\/span><\/code><\/pre>\n<\/p><\/div>\n Java8 Interface\u6539\u8fdb<\/h2>\n
\u7f16\u8bd1\u5668\u5185\u90e8\u52a0\u6cd5<\/h2>\n
\n
<\/p>\n
\n\u7c7b\u4e0e\u63a5\u53e3\u4e4b\u95f4\u7684\u5173\u7cfb<\/h3>\n
\n \u7c7b\u5b9e\u73b0\u63a5\u53e3<\/strong>\u3002\n <\/div>\n <\/p>\n
\n Java\u63a5\u53e3\u793a\u4f8b<\/h2>\n
interface printable{
void print(<\/span>);
}
class A6 implements <\/span>printable{
public void <\/span>print(<\/span>){
System.out.println<\/span><\/span>(\"Hello\"<\/span>);
}
public <\/span>static void <\/span>main(String args[]){
A6 obj = new <\/span>A6();
obj.print<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\nHello<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
Java\u63a5\u53e3\u793a\u4f8b: Drawable <\/h2>\n
\/\/Interface declaration: by first userinterface Drawable{
void draw();
}
\/\/Implementation: by second userclass Rectangle implements <\/span>Drawable{
public void <\/span>draw(){
System.out.println<\/span><\/span>(\"drawing rectangle\"<\/span>);
}
}
class Circle implements <\/span>Drawable{
public void <\/span>draw(){
System.out.println<\/span><\/span>(\"drawing circle\"<\/span>);
}
}
\/\/Using interface: by third userclass TestInterface1{
public <\/span>static void <\/span>main(String args[]){
Drawable d=new Circle();
\/\/In real scenario, object is provided by method e.g. getDrawable<\/span>()d.draw<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\ndrawing circle<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
Java\u63a5\u53e3\u793a\u4f8b: \u94f6\u884c<\/h2>\n
interface Bank{
float<\/span> rateOfInterest();
}
class SBI implements <\/span>Bank{
public <\/span>float<\/span> rateOfInterest(){
return <\/span>9.15f;
}
}
class PNB implements <\/span>Bank{
public <\/span>float<\/span> rateOfInterest(){
return <\/span>9.7f;
}
}
class TestInterface2{
public <\/span>static void <\/span>main(String[] args){
Bank b=new SBI();
System.out.println<\/span><\/span>(\"ROI: \"<\/span>+b.rateOfInterest<\/span>());
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\nROI: 9.15<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\n Java\u5728\u63a5\u53e3\u4e2d\u7684\u591a\u91cd\u7ee7\u627f<\/h2>\n
<\/p>\n
interface Printable{
void print(<\/span>);
}
interface Showable{
void show();
}
class A7 implements <\/span>Printable,Showable{
public void <\/span>print(<\/span>){
System.out.println<\/span><\/span>(\"Hello\"<\/span>);
}
public void <\/span>show(){
System.out.println<\/span><\/span>(\"Welcome\"<\/span>);
}
public <\/span>static void <\/span>main(String args[]){
A7 obj = new <\/span>A7();
obj.print<\/span>();
obj.show<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\nOutput:Hello \n
Welcome<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n Q)Java\u4e2d\u7684\u7c7b\u4e0d\u652f\u6301\u591a\u91cd\u7ee7\u627f\uff0c\u4f46\u662f\u63a5\u53e3\u53ef\u4ee5\u5b9e\u73b0\u591a\u91cd\u7ee7\u627f\uff0c\u4e3a\u4ec0\u4e48\uff1f<\/h2>\n
interface Printable{
void print(<\/span>);
}
interface Showable{
void print(<\/span>);
}
class TestInterface3 implements <\/span>Printable, Showable{
public void <\/span>print(<\/span>){
System.out.println<\/span><\/span>(\"Hello\"<\/span>);
}
public <\/span>static void <\/span>main(String args[]){
TestInterface3 obj = new <\/span>TestInterface3();
obj.print<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\nHello<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\n\u63a5\u53e3\u7ee7\u627f<\/h2>\n
interface Printable{
void print(<\/span>);
}
interface Showable extends <\/span>Printable{
void show();
}
class TestInterface4 implements <\/span>Showable{
public void <\/span>print(<\/span>){
System.out.println<\/span><\/span>(\"Hello\"<\/span>);
}
public void <\/span>show(){
System.out.println<\/span><\/span>(\"Welcome\"<\/span>);
}
public <\/span>static void <\/span>main(String args[]){
TestInterface4 obj = new <\/span>TestInterface4();
obj.print<\/span>();
obj.show<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\nHello \n
Welcome<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n\u63a5\u53e3\u4e2d\u7684Java 8\u9ed8\u8ba4\u65b9\u6cd5<\/h2>\n
interface Drawable{
void draw();
default void <\/span>msg(){
System.out.println<\/span><\/span>(\"default <\/span>method\"<\/span>);
}
}
class Rectangle implements <\/span>Drawable{
public void <\/span>draw(){
System.out.println<\/span><\/span>(\"drawing rectangle\"<\/span>);
}
}
class TestInterfaceDefault{
public <\/span>static void <\/span>main(String args[]){
Drawable d=new Rectangle();
d.draw<\/span>();
d.msg<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\ndrawing rectangle \n
default method<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n\u63a5\u53e3\u4e2d\u7684Java 8\u9759\u6001\u65b9\u6cd5<\/h2>\n
interface Drawable{
void draw();
static int cube(int x){
return <\/span>x*x*x;
}
}
class Rectangle implements <\/span>Drawable{
public void <\/span>draw(){
System.out.println<\/span><\/span>(\"drawing rectangle\"<\/span>);
}
}
class TestInterfaceStatic{
public <\/span>static void <\/span>main(String args[]){
Drawable d=new Rectangle();
d.draw<\/span>();
System.out.println<\/span><\/span>(Drawable.cube<\/span>(3));
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\ndrawing rectangle \n
27<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n Q)\u4ec0\u4e48\u662f\u6807\u8bb0\u6216\u6807\u8bb0\u63a5\u53e3\uff1f<\/h2>\n
\/\/How Serializable interface <\/span>is written?public <\/span>interface Serializable{
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Java\u4e2d\u7684\u5d4c\u5957\u63a5\u53e3<\/h3>\n
interface printable{
void print(<\/span>);
interface MessagePrintable{
void msg();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n