国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

006TheFuctionsInJAVA

2019-11-14 15:17:32
字體:
供稿:網(wǎng)友

java中,所有的函數(shù)(或稱為方法)必須在類中定義。

閑話不說,先看示例:

class Point {    int x;    int y;    Point() { // 這個是類默認(rèn)的,如果我們沒有任何構(gòu)造器,編譯器默認(rèn)給類加上這個    }    Point(int x, int y) {        this.x = x;        this.y = y;    }        /**     * 打印我的坐標(biāo)        */    void displayMyCoordinate(){        System.out.this.x + "," + this.y + ").");    }        /**     * 返回此點(diǎn)和另外一個點(diǎn)的中點(diǎn)     * @param other:另外一個點(diǎn)     */    Point midpoint(Point other){ //注意,這里的返回值類型是一個Point對象                return new Point( (this.x+other.x) /2, (this.y+other.y) /2 );            }            /**     * 返回A點(diǎn)和B點(diǎn)連線的中點(diǎn)     * @param A:點(diǎn)A     * @param B:點(diǎn)B     */    Point midpoint(Point A,Point B){ //注意,這里的返回值類型是一個Point對象,這里還用到了JAVA中的多態(tài),所謂多態(tài)簡單來說就是一個方法的多種形態(tài)。
//譬如這個方法和前述的Point midpoint(Point other)有相同的方法名,雖然名字相同單參數(shù)個數(shù)不一樣,這就是多態(tài)
//多態(tài)是為了提供不能的方法形態(tài)供調(diào)用者使用。
return new Point( (A.x+B.x) /2, (A.y+B.y) /2 ); } public static void main(String[] args) { Point p = new Point(2, 3);// 我們可以像這樣使用我們定義的構(gòu)造器構(gòu)造Point的實(shí)例p。 System.out.println("對象p中的成員變量x的值是:" + p.x); // 我們可以使用p.x訪問對象p的成員變量x System.out.println("對象p中的成員變量y的值是:" + p.y); // 我們可以使用p.y訪問對象p的成員變量y p.displayMyCoordinate(); Point A = new Point(5,6); Point B = new Point(10,20); Point C = p.midpoint(A);//獲得p、A兩點(diǎn)的中點(diǎn)C Point D = p.midpoint(A, B);//獲得A、B兩點(diǎn)的中點(diǎn)D
                      //看到?jīng)],這里就用到了前述的多態(tài)
System.out.print("p點(diǎn):" ); p.displayMyCoordinate(); System.out.print("A點(diǎn):" ); A.displayMyCoordinate(); System.out.print("B點(diǎn):" ); B.displayMyCoordinate(); System.out.print("p、A的中點(diǎn)C:" ); C.displayMyCoordinate(); System.out.print("A、B的中點(diǎn)D:" ); D.displayMyCoordinate(); }}

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东至县| 双牌县| 高台县| 广州市| 方正县| 盐池县| 拉孜县| 泗水县| 清流县| 永丰县| 大厂| 内乡县| 息烽县| 红桥区| 合肥市| 湖北省| 永新县| 石河子市| 吴桥县| 高台县| 南靖县| 西贡区| 衡南县| 禹州市| 边坝县| 永城市| 桂阳县| 垫江县| 苏尼特左旗| 武穴市| 绵竹市| 十堰市| 牡丹江市| 汶川县| 横峰县| 阜城县| 巍山| 和龙市| 突泉县| 京山县| 巫山县|