NO.1
class Lader
{
double above,bottom,height;
Lader(){}
Lader(double a,double b,double h)
{
above=a;
bottom=b;
height=h;
}
public void setAbove(double a)
{
above=a;
}
public void setBottom(double b)
{
bottom=b;
}
public void setHeight(double h)
{
height=h;
}
double computeArea()
{
return(above+bottom)*height/2.0;
}
}
public class Example
{
public static void main(String[]args)
{
Lader one=new Lader(12,13,14);
Lader two=new Lader();
two.setAbove(4);
two.setBottom(5);
two.setHeight(6);
double areaOne=one.computeArea();
double areaTwo=two.computeArea();
System.out.println(areaOne);
System.out.println(areaTwo);
one.setAbove(1);
one.setBottom(5);
one.setHeight(6);
double areaOne1=one.computeArea();
System.out.println(areaOne1);
}
}
No.2
class Test {
public static void main(String[]args) {
Person P = new Person();
Children c=new Children();
P.name="lisi";
P.age=21;
P.say();
c.name="zhangsan";
c.cry();
}
}
class Person {
String name;
int age;
public void say() {
System.out.println(name+age);
}
}
class Children {
String name;
public void cry() {
System.out.println(name+" is crying");
}
}
No.3
public class Test1 {
public static void main(String[]args) {
double max=T.max(12,252);
System.out.println(max);
double min=new T().min(12,23);
System.out.println(min);
}
}
class T {
static double max(double a,double b) {
return a>b?a:b;
}
double min(int x,int y) {
return x<y?x:y;
}
}
No.4
public class Test2 {
public static void main(String[]args) {
Tom cat=new Tom();
Jerry jerry=new Jerry(2);
System.out.println("调用f()之前,jerry的leg数:"+jerry.getLeg());
cat.f(jerry);
System.out.println("leg 数:"+jerry.getLeg());
}
}
class Jerry
{
int leg;
Jerry(int n) {
leg=n;
}
void setLeg(int n) {
leg=n;
}
int getLeg() {
return leg;
}
}
class Tom
{
void f(Jerry mouse) {
mouse.setLeg(12);
System.out.println("mouse修改了属性");
System.out.println("mouse有"+mouse.getLeg()+" 条腿!");
mouse=null;
}
}