Java之物件導向例子(一)

來源:互聯網
上載者:User

標籤:輸入   簡單   物件導向   util   定義   port   運算   model   color   

定義一個人類,給這個類定義一個從身份證擷取生日的方法,輸入身份證,擷取出生年月日

//主方法package com.hanqi.maya.model;import java.util.Scanner;public class Main {    public static void main(String[] args) {//主方法  main方法   main函數        Person per=new Person();  //執行個體化一個人        per.name="Bom";        System.out.println("請輸入身份證來擷取生日");        Scanner scanner=new Scanner(System.in);                String str=scanner.nextLine();        per.showBir(str);        }}
//類方法package com.hanqi.maya.model;import java.util.Date;import java.util.Scanner;import java.util.Scanner;//定義一個類public class Person {    //定義成員變數 即 屬性    public String name;    public String sex;    public int age;    public Date bir;    public String sfz;    //定義類方法    public String showBir(String sfz){        System.out.println("使用者的生日是:"+sfz.substring(6,14));        return sfz;    }}

 

注意:

局部變數使用時必須賦初始值

類裡的成員變數Java自動給賦值  int型初始值0 bollean型初始值false  String型初始值null  Date型初始值null

寫一個簡單的計算機

//主方法package com.hanqi.maya.model;import java.util.Scanner;public class Main {    public static void main(String[] args) {//主方法  main方法   main函數        System.out.println("請輸入第1個數字");        Scanner scanner=new Scanner(System.in);                String str=scanner.nextLine();                double a=Double.parseDouble(str);                System.out.println("請輸入運算子");        Scanner scanner2=new Scanner(System.in);                String str2=scanner.nextLine();                System.out.println("請輸入第2個數字");        Scanner scanner1=new Scanner(System.in);                String str1=scanner1.nextLine();                double b=Double.parseDouble(str1);        Calc sum=new Calc();        sum.jisuan(a,b, str2);    }}
package com.hanqi.maya.model;import java.util.Scanner;//電腦類public class Calc {    //+-*/簡單運算方法    public double jisuan (double a,double b,String f){                if(f.equals("+")){            System.out.println("a"+f+"b"+"="+(a+b));            return a+b;        }else if(f.equals("-")){            System.out.println("a"+f+"b"+"="+(a-b));            return a-b;        }else if(f.equals("/")){            System.out.println("a"+f+"b"+"="+(a/b));            return a/b;        }else if(f.equals("*")){            System.out.println("a"+f+"b"+"="+(a*b));            return a*b;        }        return 0;    }}

定義一個車類,定義一個顯示車類資訊的方法,一個根據油耗和裡程計算剩餘油量的方法

//主方法package com.hanqi.maya.model;import java.util.Scanner;public class Main {    public static void main(String[] args) {//主方法  main方法   main函數Car car=new Car("奧T","AA",15,10,30);        car.show();        car.run(25);        car.show();            }}
package com.hanqi.maya.model;public class Car {    public String pinp;    public String chex;    public double price;    public int yh;    public double lc;    public int yxrj;    public int syyl;        public Car(){//定義建構函式            }    public Car(String pinp1,String chex1,double price1,int yh1,int yxrj1){        pinp=pinp1;        chex=chex1;        price=price1;        yh=yh1;        lc=0;        yxrj=yxrj1;        syyl=yxrj1;    }    //顯示資訊的方法    public void show(){        System.out.println("品牌:"+pinp);        System.out.println("車型:"+chex);        System.out.println("價格:"+price);        System.out.println("油耗:"+yh);        System.out.println("裡程數"+lc);        System.out.println("油箱容積:"+yxrj);        System.out.println("剩餘油量:"+syyl+"L");        System.out.println();    }    //計算剩餘油量的方法    public void run(double gongls){        double d=(gongls/100)*yh;        syyl=(int) (syyl-d);        System.out.println("行駛"+d+"公裡之後");        System.out.println();    }}

 

Java之物件導向例子(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.