java 基礎

類函數

描述(輸入長寬判斷面積和周長。)

class Rectangle {
	double length;
	double width;
	double getArea() {
		return length*width;
	}
	double getPerimeter() {
		return 2*(length+width);
	}
	/*
	Rectangle(double a,double b){
		length=a;
		width=b;
	}
	*/
	double getLength(double a){
		length=a;
		return 0;
	}
	double getwidth(double b){
		width=b;
		return 0;
	}
}


import java.util.Scanner;
public class Wpb
{
	public static void main(String[] args){
		Rectangle in = new Rectangle();
		Scanner input = new Scanner(System.in);
		double a=input.nextDouble();
		double b=input.nextDouble();
		in.getLength(a);
		in.getwidth(b);
		System.out.println(in.getArea()+"\n"+in.getPerimeter());
	}
}

構造函數


class Rectangle {
	double length;
	double width;
	double getArea() {
		return length*width;
	}
	double getPerimeter() {
		return 2*(length+width);
	}
	
	Rectangle(double a,double b){
		length=a;
		width=b;
	}
	
	/*
	double getLength(double a){
		length=a;
		return 0;
	}
	double getwidth(double b){
		width=b;
		return 0;
	}
	*/
}

import java.util.Scanner;
public class Wpb
{
	public static void main(String[] args){
		Rectangle r = new Rectangle(10,5);
		System.out.println(r.getArea()+"\n"+r.getPerimeter());
	}
}

繼承


class Student extends Person
{
	String studentID="201611020188";
	String schoolName="THNU";
	void display(){
		super.display();
		System.out.println("student id is :"+studentID);
		System.out.println("school name is :"+schoolName);
	}
}

class Person{
	String personID="000000000000000000";
	String name="noname";
	int age=0;
	char  sex='M';
	Person(){};
	void display(){
		System.out.println("the id is :"+personID);
		System.out.println("name is "+name);
		System.out.println("age is "+age);
		System.out.println("sex is "+sex);
	}
}

class  PrintfStudent
{
	public static void main(String[] args) 
	{
		Student in=new Student();
		in.display();
	}
}

//--------------------


class Person{
	String personID;
	String name;
	int age;
	char sex;
	Person(String wpersonID,String wname,int wage,char  wsex){
		personID=wpersonID;
		name=wname;
		age=wage;
		sex=wsex;
	};
	/*
	void display(){
		System.out.println("the id is :"+personID);
		System.out.println("name is "+name);
		System.out.println("age is "+age);
		System.out.println("sex is "+sex);
	}
	*/
}

class Student extends Person
{
	String studentID;
	String schoolName;
	Student(String wpersonID,String wname,int wage,char wsex,String wstudentID,String wschoolName){
		super(wpersonID,wname,wage,wsex);
		studentID=wstudentID;
		schoolName=wstudentID;
	};
	/*
	void display(){
		super.display();
		System.out.println("student id is :"+studentID);
		System.out.println("school name is :"+schoolName);
	}
	*/
}

class  PrintfStudent
{
	public static void main(String[] args) 
	{
		Student in=new Student("454654","454584",45,'M',"11111","thnu");
		System.out.println("the id is :"+in.personID);
		System.out.println("name is "+in.name);
		System.out.println("age is "+in.age);
		System.out.println("sex is "+in.sex);
		System.out.println("student id is :"+in.studentID);
		System.out.println("school name is :"+in.schoolName);
	}
}


抽象函數

抽象類不能實例化不能生成對象,抽象函數是用來繼承的。

抽象函數本身沒有別的用處爲了把幾個強行類合併成一個類



public abstract class Shape
{
	public abstract double getArea();
}


public class circle extends Shape
{
	double r;
	int circle(double rr){
		r=rr;
		return 0;
	}
	public double getArea(){
		return Math.PI*r*r;
	}
}



   import java.util.Scanner;  
   public class Wpb  
   {  
       public static void main(String[] args){  
           circle in = new circle();  
           Scanner input = new Scanner(System.in);  
           double r=input.nextDouble();  
           in.circle(r);
		   String result = String .format("%.2f",in.getArea());    
           System.out.println("圓的面積\n"+result+"\n"); 
		   
		   /*double d = 3.1415926;

String result = String .format("%.2f",d);

%.2f %. 表示 小數點前任意位數   2 表示兩位小數 格式後的結果爲f 表示浮點型
*/
       }  
   } 















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章