定义一个长方形类,定义求周长和面积的方法,然后定义一个测试类,进行测试。
2、封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩,封装方法,求总分,平均分,以及打印学生的信息。
public static void main(String[] args) {
Student stu = new Student("张三",18,"男",88,92,96);
System.out.println (stu.getName () +"的总分:"+ stu.getTotalScore());
System.out.println (stu.getName ()+ "的平均分:"+ stu.getAvgScoure());
stu.printstudentInfo ();
}
}
class Student{
private String name;
private int age;
private String gender;
private double englishscore;
private double mathScore;
private double chinesescore;
/**
* 获取总分
*/
public double getTotalScore(){
return this.chinesescore+ this.englishscore + this.mathScore;
}
/**
* 获取平均分
*/
public double getAvgScoure(){
return this.getTotalScore() /3;
}
public Student() {
super();
}
public void printstudentInfo() {
System.out.println ("name=" + this.name + "\t age=" + this.age+"\t gender" +gender + "\t englishScore=" + this.englishscore
+"\t mathScore=" + this.mathScore + "\t chineseScore=" + chinesescore);
}
public Student(String name, int age, String gender, double englishscore, double mathScore, double chinesescore) {
super();
this.name = name;
this.age = age;
this.gender = gender;
this.englishscore = englishscore;
this.mathScore = mathScore;
this.chinesescore = chinesescore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getEnglishscore() {
return englishscore;
}
public void setEnglishscore(double englishscore) {
this.englishscore = englishscore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getChinesescore() {
return chinesescore;
}
public void setChinesescore(double chinesescore) {
this.chinesescore = chinesescore;
}
3、定义一个“点”(Point) x y set get类用来表示二维空间中的点。要求如下:
A可以生成具有特定坐标的点对象。
B提供可以设置坐标的方法。
C提供可以计算该点距离另一点距离的方法。
D提供可以计算三个点构成图形的面积的方法。
面积可以使用海伦公式:边长分别为a,b.c p=(a+b+c)/2
s=Math.sqrt(p(p-aX(p-b)*(p-c))
static关键字:
static修饰的变量我叫静态变呈/共享变呈/类变呈
static的特点:
1.静态变量属于某个类,而不属于某个具体的对象
2.只有静态才能访问静态,非静态变量不能够出现在静态方法中
3.访问静态成员的方式
1.类名.成员变量 ⒉.类名.成员方法
4.静态环境下不能出现this和super关键字
5.static能够修饰的成员(成员变量,成员方法,内部类)
举例
public static void main(String[] args) {
StudentBean s1 = new StudentBean ("张三",18) ;
StudentBean s2 =new StudentBean ("李四",19) ;
StudentBean s3 = new StudentBean ("王五",20);
s1.country ="中国";
s2.country ="中国";
s3.country ="中国";
StudentBean.country = "中国";
s1.show ();
s2.show();
s3.show();
}
}
class StudentBean{
private String name;
private int age;
public static String country;
public void show(){
System.out.println("name : " + name + " age:" + age + " country:" +country) ;
}
public StudentBean() {
}
public StudentBean(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}