2016/1/15 面向对象练习 1,创建类 属性 方法 构造方法 2,自定义图书类 3温度单位转换工具

 1 public class Factory {
 2 
 3 //属性
 4     //品牌
 5     String pinpai;
 6     //设备
 7     int machine;
 8     //材料
 9     double material;
10     //资金
11     double money;
12     //人工
13     int rengong;
14     //产品
15     int goods;
16     
17     void product(int xiaohao1){
18         machine=xiaohao1;
19         System.out.println("一单位产品机械消耗占比 "+xiaohao1+"%");
20     }
21     void product1(int xiaohao2){
22         material=xiaohao2;
23         System.out.println("一单位产品材料消耗占比 "+xiaohao2+"%");
24     }
25     void product2(int xiaohao3){
26         money=xiaohao3;
27         System.out.println("一单位产品资金消耗占比 "+xiaohao3+"%");
28     }
29     void product3(int time){
30         rengong=time;
31         System.out.println("一单位产品工时消耗占比 "+time+"%");
32     }
33     void product4(int chanchu){
34         goods=chanchu;
35         System.out.println("产出"+chanchu+"单位产品");
36         
37     }
38 }
View Code
 1 public class TestFactory {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // 
 8         Factory Lenovo=new Factory();
 9         Lenovo.pinpai="联想";
10         System.out.println("公司  "+Lenovo.pinpai);
11         
12         Lenovo.machine=20;
13         Lenovo.material=30;
14         Lenovo.money=30;
15         Lenovo.rengong=20;
16         Lenovo.goods=1;
17         
18         Lenovo.product(20);
19         Lenovo.product1(30);
20         Lenovo.product2(30);
21         Lenovo.product3(20);
22         Lenovo.product4(1);
23         
24         Factory acer=new Factory();
25         acer.pinpai="宏基";
26         System.out.println("公司  "+acer.pinpai);
27         acer.machine=20;
28         acer.material=20;
29         
30         
31         int xiaohao1=25;
32         acer.product(xiaohao1);
33         int xiaohao2=20;
34         acer.product1(xiaohao2);
35         int xiaohao3 = 30;
36         acer.product2(xiaohao3);
37         int time = 25;
38         acer.product3(time);
39         int chanchu = 1;
40         acer.product4(chanchu);
41         
42         
43         
44     }
45 
46 }
View Code

 1 public class Book {
 2     private String title; //定义书名
 3     private String author;//定义作者
 4     private double price;//定义价格
 5     public Book(String title,String author,double price){
 6         this.title=title;     //利用构造方法初始化域
 7         this.author=author;
 8         this.price=price;
 9 }
10     public String getTitle(){   //获得书名
11         return title;
12     }
13     public String getAuthor(){    //获得作者
14         return author;
15     }
16     public double getprice(){    //获得价格
17         return price;
18     }
19     
20 }
View Code
 1 public class TestBook {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         Book book=new Book("《Java从入门到精通(第2版)》","明日科技",59.8);//创建对象
 8         System.out.println("书名:"+book.getTitle());  //输出书名
 9         System.out.println("作者:"+book.getAuthor()); //输出作者
10         System.out.println("价格:"+book.getprice()+"元");//输出价格
11     
12     }
13 
14 }
View Code

 1 import java.util.Scanner;
 2 
 3 
 4 public class CelsiusConverter {
 5 
 6     public double getFahrenheit(double celsius){
 7         double fahrenheit=1.8*celsius+32;            //计算华氏温度
 8         return fahrenheit;                            //返回华氏温度
 9     }
10     public static void main(String[] args) {
11         
12         System.out.println("请输入要转换的温度(单位:摄氏度)");
13         Scanner in=new Scanner(System.in);//获得控制台输入
14         double celsius=in.nextDouble();  //获得用户输入的摄氏温度
15         CelsiusConverter converter =new CelsiusConverter();//创建类的对象
16         double fahrenheit=converter.getFahrenheit(celsius);//转换温度为华氏度
17         System.out.println("转换完成的温度(单位:华氏度):"+fahrenheit);//输出转换结果
18     }
19 
20 }
View Code

原文地址:https://www.cnblogs.com/haodayikeshu/p/5134768.html