类函数
描述(输入长宽判断面积和周长。)
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 表示浮点型
*/
}
}