package com.ll;
public class Class3 {
private String name;
private int age;
private int no;
private double r;//声明圆的半径
public Class3(String a,int b,int c){
name=a;
age=b;
no=c;//带参数的构造方法
}//半径的构造方法
public String toString (){
String a="我叫"+name+",我的年龄"+age+",我的学号"+no;
return a;
}
public Class3(double d){
r=d;
}
public Class3(){}
//求圆的面积的方法
public double area (double r){
double s=Math.PI*r*r;
return s;
}
}
(测试类)
package com.ll;
public class test {
public static void main(String[] args) {
Class3 ho=new Class3("陈浩",23,32);
Class3 h2=new Class3();
System.out.println("计算圆的面积为:"+h2.area(23.2));
System.out.println(ho.toString());//总结下,写程序必要一个无参构造方法和一个普通方法(赋初值),还有new对象,调用私有方法
}
}