4.1
class XiyoujiRenwu {
float height,weight;
String head,ear;
void speak(String s) {
System.out.println(s);
}
}
public class Example4_1 {
public static void main(String args[]) {
XiyoujiRenwu zhubajie,sunwukong; //声明对象
zhubajie = new XiyoujiRenwu(); //为对象分配变量(使用new和默认的构造方法)
sunwukong = new XiyoujiRenwu();
}
}
4.2
public class Example4_2 { //应用程序的主类
public static void main(String args[]){
XiyoujiRenwu zhubajie=null, sunwukong=null; //声明对象
zhubajie=new XiyoujiRenwu(); //为对象分配内存,使用new运算符和默认的构造方法
sunwukong=new XiyoujiRenwu();
zhubajie. name=PersonName.八戒; //对象zhubajie给自己的变量赋值
zhubajie. height=1.83f;
zhubajie. weight=86f;
zhubajie. head="猪头";
sunwukong. name=PersonName.悟空; //对象sunwukong给自己的变量赋值
sunwukong. height=1.66f;
sunwukong. weight=1000f;
sunwukong. head="猴头";
System. out. println(zhubajie. name+"的身高:"+zhubajie. height);
System. out. println(zhubajie. name+"的体重:"+zhubajie. weight);
System. out. println(zhubajie. name+"的头:"+zhubajie. head);
System. out. println(sunwukong. name+"的身高:"+sunwukong. height);
System. out. println(sunwukong. name+"的体重:"+sunwukong. weight);
System. out. println(sunwukong. name+"的头:"+sunwukong. head);
zhubajie. speak(zhubajie. name+"我想娶媳妇"); //对象调用方法
System. out. println(zhubajie. name+"现在的头:"+zhubajie. head);
sunwukong. speak(sunwukong. name+"我重"+
sunwukong. weight+"公斤,想骗八戒背我"); //对象调用方法
System. out. println(sunwukong. name+"现在的头:"+sunwukong. head);
}
}
4.3
class Point {
int x,y;
void setXY(int m, int n){
x = m;
y = n;
}
}
public class Example4_3{
public static void main(String args[]){
Pointp1,p2;
p1 = new Point();
p2 = new Point();
System. out. println("p1的引用:"+p1);
System. out. println("p2的引用:"+p2);
p1.setXY(1111,2222);
p2.setXY( -100,-200);
System. out. println("p1的x,y坐标:"+p1.x+","+p1.y);
System. out. println("p2的x,y坐标:"+p2.x+","+p2.y);
p1=p2;
System. out. println("将p2的引用赋给p1后:");
System. out. println("p1的引用:"+p1);
System. out. println("p2的引用:"+p2);
System. out. println("p1的x,y坐标:"+p1.x+","+p1.y);
System. out. println("p2的x,y坐标:"+p2.x+","+p2.y);
}
}
4.4
public class Example4_4 { //主类
public static void main(String args[]) {
Rect rect= new Rect();
double w= 12.76,h=25.28;
rect.setWidth(w);
rect.setHeight(h);
System. out. println("矩形对象的宽:"+rect.getWidth()+"高:"+ rect.getHeight());
System. out. println("矩形的面积:"+rect.getArea());
System. out. println("更改向对象的方法参数传递值的w、h变量的值为100和256");
w= 100;
h=256;
System. out. println("矩形对象的宽:"+rect.getWidth()+"高:"+ rect.getHeight());
}
}
4.6
public class Example4_6 {
public static void main(String args[]) {
Computer computer=new Computer();
double result= computer.getResult(1.0/3,10,20,30);//"参数代表"x代表了3个参数System. out.
println("10,20,30的平均数:"+result);
result= computer.getResult(1.0/6,66,12,5,89,2,51);//"参数代表"x代表了6个参数System. out.
println("66,12,5,89,2,51的平均数:"+ result);
}
}
4.8
public class Example4_8{
public static void main(String args[]) {
Ladder.下底= 100; //Ladder的字节码被加载到内存,通过类名操作类变量
Ladder ladderOne = new Ladder();
Ladder ladderTwo= new Ladder();
ladder0ne.设置上底(28);
ladderTwo.设置上底(66);
System. out. println("ladder0ne 的上底:"+ ladder0ne.获取上底());
System. out. println("ladderOne 的下底:"+ ladder0ne.获取下底());
System. out. println("ladderTwo的上底:"+ ladderTwo.获取上底());
System. out. println("ladderTwo的下底: "+ladderTwo..获取下底());
}
}
4.9
class A{
int x,y,z;
static int getContinueSum(int start, int end){
int sum=0;
for(int i=start;i<=end;i++) {
sum=sum+i;
}
return sum;
}
}
public class Example4_9{
public static void main(String args[]){
int result=A,getContinueSum(0,100);
System. out. println(result);
}
)
4.10
public class People{
int leg, hand;
String name;
People(String s){
name=s;
this. init(); //可以省略this,即将"this. init();"写成"init();"
}
void init(){
leg=2;
hand=2;
System. out. println(name+"有"+hand+"只手"+leg+"条腿");
}
public static void main(String args[]){
People boshi=new People("布什"); //创建boshi时,构造方法中的this就是对象boshi
}
}
4.16
class AAA {
private int money;
private int getMoney(){
return money;
}
}
class E {
public static void main(String args[]) {
AAA exa = new AAA(); //对象exa在E类中,不在AAA类中
exa. money= 3000;
int m = exa.getMoney(); //非法
System. out. println("money="+m); //非法
}
}
4.17
public class Example4_17 {
public static void main(String args[]) {
Student zhang= new Student();
Student geng= new Student();
zhang.setAge(23);
System. out. println("zhang的年龄:"+zhang.getAge());
geng. setAge(25);
//"zhang. age =23;"或"geng. age=25;"都是非法的,因为zhang和geng已经不在Student类中
System. out. println("geng的年龄:"+geng.getAge());
}
}
5.编程题
(1)
class AAA {
static int m ;
static {
m=888;
}
}
public class E {
public static void main(String args[]) {
AAA a=null;
System.out.printf("gd:号d",AAA.m,a.m);
}
}
(2)
(2)
//CPU. java
public class CPU {
int speed;
int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
//HardDisk.java
public class HardDisk{
int amount;
int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
//pC.java
public class PC {
CPU cpu;
HardDisk HD;
void setCPU(CPU cpu) {
this.cpu=cpu;
}
void setHardDisk(HardDisk HD) {
this.HD= HD;
}
void show(){
System.out.println("CPU速度:"+cpu.getSpeed
System.out.println("硬盘容量:"+HD.getAmount
}
}
//Test.java
public class Test {
public static void main(String args[]) {
CPU cpu = new CPU();
HardDisk HD=new HardDisk();
cpu.setSpeed(2200);
HD.setAmount(200);
PC pc = new PC();
pc. setCPU(cpu);
pc.setHardDisk(HD);
pc.show();
}
}