import java . io . * ;
import java . util . * ;
import java . util . regex . * ;
interface myinterface
{
public void output ( ) ;
}
public class tmp implements myinterface
{
public void output ( )
{
System . out. println ( "output" ) ;
}
public static void main( String args[ ] ) throws Exception
{
myinterface t = new tmp( ) ;
t. output ( ) ;
}
}
java-interface(接口),多重继承,接口扩展-的用法
范例1:
interface ishape
{
final double pi=3.14;
abstract void area();
}
class rectangle implements ishape
{
int w,h;
public rectangle(int w1,int h1)
{
w=w1;
h=h1;
}
public void area()
{
System.out.println(“area=”+w*h);
}
}
class circle implements ishape
{
double radius;
public circle(double r)
{
radius=r;
}
public void area()
{
System.out.println(“area=”+pi*radius*radius);
}
}
public class interfacec
{
public static void main(String args[])
{
rectangle myrectangle=new rectangle(5,10);
myrectangle.area();
circle mycircle=new circle(2.0);
mycircle.area();
}
}
范例2:
interface ishape
{
final double pi=3.14;
abstract void area();
}
class rectangle implements ishape
{
int w,h;
public rectangle(int w1,int h1)
{
w=w1;
h=h1;
}
public void area()
{
System.out.println(“area=”+w*h);
}
}
class circle implements ishape
{
double radius;
public circle(double r)
{
radius=r;
}
public void area()
{
System.out.println(“area=”+pi*radius*radius);
}
}
public class interfacec
{
public static void main(String args[])
{
ishape shape1,shape2;
shape1=new rectangle(5,10);
shape1.area();
shape2=new circle(2.0);
shape2.area();
}
}
范例3:多重继承:
interface ishape
{
final double pi=3.14;
abstract void area();
}
interface ishape1
{
abstract void show();
}
class rectangle implements ishape,ishape1
{
int w,h;
public rectangle(int w1,int h1)
{
w=w1;
h=h1;
}
public void area()
{
System.out.println(“area=”+w*h);
}
public void show()
{
System.out.println(“this is rectangle class”);
}
}
class circle implements ishape,ishape1
{
double radius;
public circle(double r)
{
radius=r;
}
public void area()
{
System.out.println(“area=”+pi*radius*radius);
}
public void show()
{
System.out.println(“this is circle class”);
}
}
public class interfacec
{
public static void main(String args[])
{
rectangle myrectangle=new rectangle(5,10);
myrectangle.show();
myrectangle.area();
circle mycircle=new circle(2.0);
mycircle.show();
mycircle.area();
}
}
范例4:接口的扩展:
interface ishape1
{
final double pi=3.14;
abstract void setcolor(String strcolor);
}
interface ishape2 extends ishape1
{
abstract void area();
}
class circle implements ishape2
{
double radius;
String color;
public circle(double r)
{
radius=r;
}
public void setcolor(String strcolor)
{
color=strcolor;
System.out.println(“color=”+color);
}
public void area()
{
System.out.println(“area=”+pi*radius*radius);
}
}
public class interfacec
{
public static void main(String args[])
{
circle mycircle=new circle(2.0);
mycircle.setcolor(“red”);
mycircle.area();
}
}
请比较范例1,和范例2,掌握接口的用法,在声明接口时必须注意以下两点: |
1:接口的数据成员必须初始化,例如:范例1中:final double pi=3.14;//final可以省略 |
2:接口里的method必须全部声明成为abstract.也是只做声明不处理.例如,范例1中:abstract void area(); |
几个类如果大致的方法是一样的就可以把他们的共同的方法提出来,做成interface ,让这几个类实现这个nterface,通过interface 接口下塑造型访问这几个类。
今天的文章java interface 用法分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/25136.html