java编程接口程序

java编程接口程序,第1张

public interface Shape {

    

    double perimeter = 0

    double area = 0

    

    public abstract double calPerimeter()

    public abstract double calArea()

    

    public double getPerimeter()

    public double getArea()

}

public class Circular implements Shape {

    

    private final double PI = 3.1415926

    

    private double Radius

    

    public Circular(double radius) {

        this.Radius = radius

    }

    @Override

    public double calPerimeter() {

        // TODO Auto-generated method stub

        return Radius * PI * 2

    }

    @Override

    public double calArea() {

        // TODO Auto-generated method stub

        return Radius * Radius * PI

    }

    public double getRadius() {

        return Radius

    }

    public void setRadius(double radius) {

        Radius = radius

    }

    public double getPI() {

        return PI

    }

    @Override

    public double getPerimeter() {

        // TODO Auto-generated method stub

        return calPerimeter()

    }

    @Override

    public double getArea() {

        // TODO Auto-generated method stub

        return calArea()

    }

}

public class ShapeTestDemo {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        Circular cir = new Circular(3)

        System.out.println(cir.calArea())

        System.out.println(cir.calPerimeter())

        System.out.println(cir.getPI())

        System.out.println(cir.getArea())

    }

}

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

要能在Eclipse下运行通过。感激涕零!

解析:

很简单啊,我给你一个java类库里的接口怎样啊?是一个经常用到的MouseListener:

/*

* @(#)MouseListener.java 1.17 03/12/19

*

* Copyright 2004 Sun Microsystems, Inc. All rights reserved.

* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

*/

package java.awt.event

import java.util.EventListener

/**

* The listener interface for receiving "interesting" mouse events

* (press, release, click, enter, and exit) on a ponent.

* (To track mouse moves and mouse drags, use the

* <code>MouseMotionListener</code>.)

* <P>

* The class that is interested in processing a mouse event

* either implements this interface (and all the methods it

* contains) or extends the abstract <code>MouseAdapter</code>class

* (overriding only the methods of interest).

* <P>

* The listener object created from that class is then registered with a

* ponent using the ponent's <code>addMouseListener</code>

* method. A mouse event is generated when the mouse is pressed, released

* clicked (pressed and released). A mouse event is also generated when

* the mouse cursor enters or leaves a ponent. When a mouse event

* occurs, the relevant method in the listener object is invoked, and

* the <code>MouseEvent</code>is passed to it.

*

* @author Carl Quinn

* @version 1.17, 12/19/03

*

* @see MouseAdapter

* @see MouseEvent

* @see <a href="java.sun/docs/books/tutorial/post1.0/ui/mouselistener">Tutorial: Writing a Mouse Listener</a>

* @see <a href="awl/cp/javaseries/jcl1_2">Reference: The Java Class Libraries (update file)</a>

*

* @since 1.1

*/

public interface MouseListener extends EventListener {

/**

* Invoked when the mouse button has been clicked (pressed

* and released) on a ponent.

*/

public void mouseClicked(MouseEvent e)

/**

* Invoked when a mouse button has been pressed on a ponent.

*/

public void mousePressed(MouseEvent e)

/**

* Invoked when a mouse button has been released on a ponent.

*/

public void mouseReleased(MouseEvent e)

/**

* Invoked when the mouse enters a ponent.

*/

public void mouseEntered(MouseEvent e)

/**

* Invoked when the mouse exits a ponent.

*/

public void mouseExited(MouseEvent e)

}

接口与类的写法差不多,这个接口放在MouseListener.java(称为一个编辑单元)里.

不知道怎么给你说清楚

形象一点,c语言是面向过程编程,就是按程序执行的顺序编

而c++和java都是面向对象编程,他们把变量和方法都封装到类里面,通过对象执行程序

但是c++中的类是可以继承的,而JAVA的不能继承,只能通过实现接口来完成类似于c++继承的功能,从这个角度看java是面向接口编程


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11502210.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存