Java的初步使用
2017-10-23 17:57
274 查看
根据UML图,定义抽象账单类Bill、子类水费账单类WaterBill
求圆的面积
了解父类,子类的调用关系
package Bill; import java.util.Scanner;//花费时间四十,包含查询如何使用继承 public class BillWaterBill { public static void main(String[] args) { Scanner input = new Scanner(System.in); double volume =input.nextDouble(); // Bill bill = new Bill(); // bill.getVolume(volume); WaterBill waterbill = new WaterBill(volume); waterbill.getBill(); } } class Bill{ double volume;//使用量 public Bill(){}//这里使用public 还不确定 public Bill(double volume){ this.volume = volume; } double getVolume(double volume) { this.volume = volume; return volume; } double setVolume(double volume) { return volume; } public double getBill() {//计算bill return this.volume; } } class WaterBill extends Bill{//能够继承public 以及protected private double[] rate = {2.28,2.5,3.9}; WaterBill(){//系统会自动调用子类无参构造函数 } WaterBill(double volume){//uml图中,+,-,#分别表示 public ,private,protected this.volume = volume; } // UML图包括三个部分 // 1:类名 //2:类的属性,属性是指类的成员变量,UML属性的表示方式为:可见性 名称:类型 [ = 缺省值 ] //可见性就是私有公有保护 // 名称就是属性名,用一个字符串表示 // 类型就是数据类型,也可以是用户自定义的名称 // 缺省值,是一个可选项,就是属性的初始值 // object 对象 // 3:类的操作:操作是类的任意一个势力对象都可以使用的行为,是类的成员方法,UML规定操作的表示方法为:可见性 名称(参数列表) [ : 返回类型] double[] getRate() { return this.rate; } void setRate(double[] rate) { this.rate = rate; } public double getBill() { double bill = 0; if(this.volume>260) { bill = (this.volume - 260) * this.rate[2] + 80 * rate[1] + 180 * rate[0]; } else if(this.volume > 180) { bill = (this.volume - 180) * rate[1] + 180 * rate[0]; } else bill = (this.volume) * rate[0]; System.out.println("水费为"+bill); return bill; } }
求圆的面积
package Circle; import java.util.Scanner; class Circle{ private double radius; Circle(){ } Circle(double radius){ this.radius = radius ; } double getRadius() { return radius ; } void setRadius(double speed) { this.radius = speed; } double getArea() { return radius*radius*3.14;//结果与答案相差几位小数 应该是估读的问题 但修改为3.14一样有相差 } } //声明静态变量-- 让一个类的所有实例共享数据 //定义方法:在定义前加static //使用private,定义方法还有变量,达到数据封装的效果 //UML类图中,使用“-”号表示,private //英语好好学,计算机专业很多的内容需要用到英语文献 //对象数组初始化方法 //Circle[] circleArray = new Circle[10]; //for(int i = 0;i<10;i++) // circleArray[i] = new Circle(); public class CircleArea { public static void main(String[] args) { // TODO Auto-generated method stub // System.out.println("hello world"); // System.out.println(c.length); // sum(createCircleArray()); // String a;//定义字符串 // a = "new string"; // System.out.println(a); // resolved 决定 String newString = "welcome to java"; // String newString = new String("welcome to Java"); System.out.println(newString); // 字符串在Java中是不可变的,所以定义之后是不能改变的,就是 newString = "hello world!"; System.out.println(newString); System.out.println(newString.length()); // printCircleArray(createCircleArray()); } // Create class of circle //返回一个数组大小为三 public static Circle[] createCircleArray() { //创建圆类型数组 Circle [] circle = new Circle[3]; Scanner input = new Scanner(System.in); for(int x1 = 0 ;x1<circle.length;x1++) { double x2 = input.nextDouble(); // System.out.println(circle[x1]); circle[x1] = new Circle(); //数组初始化了,指针不为零,但其中的对象还没有初始化 circle[x1].setRadius(x2); } return circle; } public static double sum(Circle[] circles) { double sum1 = 0; for(int x = 0 ;x<circles.length;x++) sum1+=circles[x].getArea(); return sum1; } public static void printCircleArray(Circle[] circles) { System.out.println("半径 面积"); for(int x = 0;x<circles.length;x++) System.out.println(circles[x].getRadius()+" "+circles[x].getArea()); System.out.println(); System.out.println("总面积为"+sum(circles)); } }
了解父类,子类的调用关系
package Person; import java.util.Scanner; public class PersonEmplyee { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); String name1 = input.nextLine(); String address1 = input.nextLine();//想要知道怎么通过空格来读取 String phone1 = input.nextLine(); String email1 = input.nextLine(); Person person = new Person(name1,address1,phone1,email1); String name2 = input.nextLine(); String address2 = input.nextLine(); String phone2 = input.nextLine(); String email2 = input.nextLine(); String office2 = input.nextLine(); int salary2 = input.nextInt(); Employee employee = new Employee(name2,address2,phone2,email2,office2,salary2); String name3 = input.nextLine(); String address3 =input.nextLine(); String phone3 = input.nextLine(); String email3 = input.nextLine(); String office3 = input.nextLine(); int salary3 = input.nextInt(); int work3 = input.nextInt(); String title3 = input.nextLine(); Faculty faculty = new Faculty(name3,address3,phone3,email3,office3,salary3,work3,title3); person.toString(); employee.toString(); faculty.toString(); } } class Person{ private String name,address,phone,email; Person(){} Person(String name,String address,String phone,String email){ this.name = name; this.address = address; this.phone = phone; this.email =email; } public String toString() { System.out.println("姓名"+this.name); System.out.println("地址"+this.address); System.out.println("电话号码"+this.phone); System.out.println("电子邮件"+this.email); return "skdjf"; } } class Employee extends Person{ private String office; private int salary; Employee(){} Employee(String name,String address,String phone,String email,String office,int salary){ super(name,address,phone,email);//调用父类的构造方法 this.office = office; this.salary = salary; } public String toString() { super.toString();//使用super调用父类的方法 System.out.println("办公室"+this.office); System.out.println("工资"+this.salary); System.out.println(); return "aksjfd"; } } class Faculty extends Employee { public int TEACHINGASSISTANT = 1; public int LECTERER = 2; public int ASSOCIATE = 4; public int PROFESSOR = 4; private int workingDayAndTime; private String title; Faculty(){} Faculty(String name,String address,String phone,String email,String office,int salary,int workingDayAndTime,String title){ super(name,address,phone,email,office,salary); this.workingDayAndTime = workingDayAndTime; this.title = title; } public String toString () { super.toString(); System.out.println("办公时间"+this.workingDayAndTime); System.out.println("职称"+this.title); return "sldfj"; } }
相关文章推荐
- HSQLDB: java程序使用hsqldb 入门教程 java启动hsqldb (初步hsqldb效率评估)
- java 断言 assert 初步使用:断言开启、断言使用
- Memcached学习笔记——windows上初步使用(与java结合)
- Memcached学习笔记——windows上初步使用(与java结合)
- java 断言 assert 初步使用:断言开启、断言使用
- 11级_Java_曹建波 04.23 初步认识Spring,为什么要使用Spring及优势
- java 断言 assert 初步使用:断言开启、断言使用
- 使用java/groovy扩展ant初步
- 初步解析Java中AffineTransform类的使用
- java 断言 assert 初步使用:断言开启、断言使用
- java I/O流初步认识使用
- 简单完整的代码,通过这个代码你将对RSA加密算法在Java中的实现方法有一个初步的了解,这个类,你可以直接使用,水平高的,就自己修改完善下代码。
- java入门学习(12)—内部类的定义及使用初步
- JAVA使用JDBC编程的初步认识
- [Java Path Finder][JPF学习笔记][6]jpf-symbc的初步使用
- Java中有返回值的线程,(缓存)线程池的初步使用
- windows 上memcached初步使用-java版
- Redis学习记录之Java中的初步使用
- 使用C#调用Java带MIME附件WebService方法的初步设想
- Java处理http协议相关初步(三)——线程池的使用分析