新聞中心
用Java語(yǔ)言編寫一個(gè)類求圓的周長(zhǎng)
可以通過(guò)創(chuàng)建一個(gè)圓的類完成計(jì)算圓周長(zhǎng)和面積的功能。
創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元崇仁做網(wǎng)站,已為上家服務(wù),為崇仁各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
假設(shè)這個(gè)圓的類名叫做Circle,因?yàn)楦鶕?jù)圓的半徑就可以求出圓的周長(zhǎng)和面積,所以可以在這個(gè)類中定義一個(gè)半徑屬性mRadius,然后定義兩個(gè)方法getLength和getArea分別實(shí)現(xiàn)計(jì)算圓周長(zhǎng)和面積的功能。
java語(yǔ)言源代碼如下:
public class Circle{
//圓的半徑
private double mRadius;
public Circle(double mRadius){
this.mRadius = mRadius;
}
//獲取圓的周長(zhǎng)
public double getLength(){
return 2*Math.PI*mRadius;
}
//獲取圓的面積
public double getArea(){
return Math.PI*mRadius*mRadius;
}
}
//注:由于測(cè)試類只是調(diào)用Circle類的方法,功能很簡(jiǎn)單,便沒有寫測(cè)試類。
Java語(yǔ)言編寫代碼
代碼如下
/**
*?Author:?zhyx
*?Date:2017/11/30
*?Time:8:56
*/
public?abstract?class?Contailner?{
double?r;
abstract?double?volume();
}
/**
*?Author:?zhyx
*?Date:2017/11/30
*?Time:8:57
*/
public?class?Cube?extends?Contailner?{
public?Cube(double?r)?{
this.r=r;
}
@Override
double?volume()?{
return?r*r*r;
}
}
/**
*?Author:?zhyx
*?Date:2017/11/30
*?Time:9:01
*/
public?class?Sphere?extends?Contailner?{
public?Sphere(double?r)?{
this.r=r;
}
@Override
double?volume()?{
return?4/3*Math.PI*r*r*r;
}
}
/**
*?Author:?zhyx
*?Date:2017/11/30
*?Time:9:02
*/
public?class?Tiji?{
public?static?void?main(String[]?args)?{
Cube?cube=new?Cube(4);
System.out.println("立方體體積為:"+cube.volume());
Sphere?sphere=?new?Sphere(4);
System.out.println("球體體積為:"+sphere.volume());
}
}
用java編寫一個(gè)簡(jiǎn)單例子,題目如下
package test;
public class Student {
private String name;
private String id;
private String clazz;
private int age;
private String address;
/**
* sayHello方法
*/
public void sayHello() {
System.out.println("學(xué)號(hào)為" + this.id + "的同學(xué)的具體信息如下:");
System.out.println("姓名:" + this.name);
System.out.println("班級(jí):" + this.clazz);
System.out.println("年齡:" + this.age);
System.out.println("家庭住址:" + this.address);
}
/**
* 測(cè)試方法
*
* @param args
*/
public static void main(String[] args) {
// 第一問
Student student = new Student();
student.setAddress("百度知道");
student.setAge(1);
student.setClazz("一班");
student.setId("071251000");
student.setName("lsy605604013");
student.sayHello();
// 第二問
Student studentNew = new Student();
studentNew.setAddress("搜搜知道");
studentNew.setAge(2);
studentNew.setClazz("二班");
studentNew.setId("071251001");
studentNew.setName("lady");
if (student.getAge() studentNew.getAge())
studentNew.sayHello();
else if (student.getAge() studentNew.getAge())
student.sayHello();
else
System.out.println("兩人一樣大");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
網(wǎng)頁(yè)標(biāo)題:java語(yǔ)言代碼例子 java語(yǔ)言實(shí)例
網(wǎng)址分享:http://www.ef60e0e.cn/article/hpgdcd.html