⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 101.txt

📁 Java 2实用教程(第三版) 清华大学出版社 (编著 耿祥义 张跃平) 例子源代码 第四章
💻 TXT
📖 第 1 页 / 共 2 页
字号:
第四章	类、对象和接口
例子1
class XiyoujiRenwu 
{   float height,weight;
    String head, ear,hand,foot, mouth;
    void speak(String s) 
{  System.out.println(s);
    }
}
class A 
{   public static void main(String args[]) 
 {  XiyoujiRenwu  zhubajie;       //声明对象
       zhubajie=new  XiyoujiRenwu(); //为对象分配内存,使用new 运算符和默认的构造方法
    }
}
例子2
class Point
 {  int x,y;
    Point(int a,int b) 
 {  x=a;
       y=b;
    }
}
public class A 
{  public static void main(String args[]) 
 {  Point p1,p2;                 //声明对象p1和p2
      p1=new Point(10,10);         //为对象分配内存,使用 new 和类中的构造方法
      p2=new Point(23,35);        //为对象分配内存,使用 new 和类中的构造方法
}
}
例子3
class XiyoujiRenwu 
{   float height,weight;
    String head, ear,hand,foot,mouth;
void speak(String s) 
{  head="歪着头";
       System.out.println(s);
    }
}
class Example4_3 
{    public static void main(String args[])
   {  XiyoujiRenwu  zhubajie,sunwukong;//声明对象
        zhubajie=new  XiyoujiRenwu(); //为对象分配内存
    sunwukong=new  XiyoujiRenwu();
        zhubajie.height=1.80f;     //对象给自己的变量赋值
        zhubajie.head="大头"; 
        zhubajie.ear="一双大耳朵"; 
        sunwukong.height=1.62f;    //对象给自己的变量赋值
        sunwukong.weight=1000f;   
        sunwukong.head="绣发飘飘"; 
        System.out.println("zhubajie的身高:"+zhubajie.height);
        System.out.println("zhubajie的头:"+zhubajie.head);
        System.out.println("sunwukong的重量:"+sunwukong.weight);
        System.out.println("sunwukong的头:"+sunwukong.head);
        zhubajie.speak("俺老猪我想娶媳妇");      //对象调用方法
        System.out.println("zhubajie现在的头:"+zhubajie.head);
        sunwukong.speak("老孙我重1000斤,我想骗八戒背我"); //对象调用方法
        System.out.println("sunwukong现在的头:"+sunwukong.head);
   }
}
例子4
class 梯形
{   float 上底,下底,高,面积;
    梯形(float x,float y,float h) 
{  上底=x;
       下底=y;
       高=h;
    }
    float 计算面积()
 {  面积=(上底+下底)*高/2.0f;
       return 面积;
    }
    void 修改高(float height)
 {  高=height;
    }
    float 获取高()
 {  return 高;
    }
}
public class Example4_4 
{  public static void main(String args[])
 {  梯形 laderOne=new 梯形(12.0f,3.5f,50),laderTwo=new 梯形(2.67f,3.0f,10);
      System.out.println("laderOne的高是:"+laderOne.获取高());
      System.out.println("laderTwo的高是:"+laderTwo.获取高());
      System.out.println("laderOne的面积是:"+laderOne.计算面积());
      System.out.println("laderTwo的面积是:"+laderTwo.计算面积());
      laderOne.修改高(10);
      float h=laderOne.获取高();
      laderTwo.修改高(h*2);
      System.out.println("laderOne现在的高是:"+laderOne.获取高());
      System.out.println("laderTwo现在的高是:"+laderTwo.获取高());
      System.out.println("laderOne现在的面积是:"+laderOne.计算面积());
      System.out.println("laderTwo现在的面积是:"+laderTwo.计算面积());
    } 
} 
例子5
class People
{  String face;
   void setFace(String s)
 {  face=s;
   }
}
class A 
{  void f(int x,double y,People p)
 {  x=x+1;
      y=y+1;
      p.setFace("笑脸");
      System.out.println("参数x和y的值分别是:"+x+","+y);
      System.out.println("参数对象p的face是:"+p.face);
   } 
}
public class Example4_5 
{  public static void main(String args[])
{  int x=100;
      double y=100.88; 
      People zhang=new People();
      zhang.setFace("很严肃的样子");
      A a=new A();
      a.f(x,y,zhang); 
      System.out.println("main方法中x和y的值仍然分别是:"+x+","+y);
      System.out.println("main方法中对象zhang的face是:"+zhang.face);
   }
}
例子6
class 圆  
{  double 半径;
   圆(double r) 
 {   半径=r;
}
  double 计算面积() 
{   return 3.14*半径*半径;
   } 
  void 修改半径(double 新半径)
  {   半径=新半径;
   }
  double 获取半径()
  {   return 半径;
   }
}
class 圆锥
{  圆 底圆;
   double 高;
   圆锥(圆 circle,double h)
 {   this.底圆=circle;
       this.高=h;
   }
  double 计算体积()
 {   double volume;
      volume=底圆.计算面积()*高/3.0;
      return  volume;
  }
  void 修改底圆半径(double r)
 {   底圆.修改半径(r);
  }
  double 获取底圆半径()
 {   return 底圆.获取半径();
  }
}
class Example4_6 
{  public static void main(String args[]) 
 {  圆 circle=new 圆(10);
      圆锥 circular=new 圆锥(circle,20);
      System.out.println("圆锥底圆半径:"+circular.获取底圆半径()); 
      System.out.println("圆锥的体积:"+circular.计算体积());
      circular.修改底圆半径(100);
      System.out.println("圆锥底圆半径:"+circular.获取底圆半径());
      System.out.println("圆锥的体积:"+circular.计算体积());  
   }
}
例子7
class 梯形  
{   float 上底,高;
    static float 下底;
梯形(float x,float y,float h) 
{   上底=x; 下底=y; 高=h;
    }
float 获取下底()
{   return 下底;
    }
void 修改下底(float b)
{   下底=b; 
    }
}
class Example4_7 
{   public static void main(String args[])
  {  梯形 laderOne=new 梯形(3.0f,10.0f,20),laderTwo=new 梯形(2.0f,3.0f,10);
       梯形.下底=200;                 //通过类名操作类变量
       System.out.println("laderOne的下底:"+laderOne.获取下底());
       System.out.println("laderTwo的下底:"+laderTwo.获取下底());
       laderTwo.修改下底(60);         //通过对象操作类变量
       System.out.println("laderOne的下底:"+laderOne.获取下底());
       System.out.println("laderTwo的下底:"+laderTwo.获取下底());
    } 
}
例子8
class Fibi
{   public static long  fibinacii(int n) 
{   long c=0;
        if(n==1||n==2)
           c=1;
        else
           c=fibinacii(n-1)+fibinacii(n-2);
         return c;
    }
}
public class Example4_8 
{  public static void main(String args[])
 {  System.out.println(Fibi.fibinacii(7));
   }
}
例子9
package tom.jiafei;
public class PrimNumber 
{   public void getPrimnumber(int n)
{  int sum=0,i,j;
       for(i=1;i<=n;i++)  
{  for(j=2;j<=i/2;j++)
 {  if(i%j==0)
                   break;
          }
          if(j>i/2) 
            System.out.print(" "+i);
       }
     }
public static void main(String args[])
{  PrimNumber p=new PrimNumber();
       p.getPrimnumber(20); 
    }
}
例子10
import java.applet.Applet;
import java.awt.*;
public class Example4_10 extends Applet
{   Button redbutton;
    public void init()
 {    redbutton=new Button("我是一个红色的按钮"); 
         redbutton.setBackground(Color.red);
redbutton.setForeground(Color.white);
         add(redbutton);
     }
}
例子11
import tom.jiafei.*; //引入包tom.jiafei中的类
public class Example4_11 
{  public static void main(String args[])
 {  PrimNumber num=new PrimNumber();//用包tom.jiafei中的类创建对象
      num.getPrimnumber(30);
   }
}
例子12
public class Example4_12
 {  public static void main(String args[])
 {  PrimNumber num=new PrimNumber();//要保证PrimNuber类和Example4_12类在同一目录中
       num.getPrimnumber(120);
    }
     }
Trangel.java
package tom.jiafei;
public class Trangle 
{   double sideA,sideB,sideC;
    boolean boo;
    public Trangle(double a,double b,double c)
 {  sideA=a;
       sideB=b;
       sideC=c;
       if(a+b>c&&a+c>b&&c+b>a) 
{  System.out.println("我是一个三角形");
          boo=true;
       }    
      else 
 {  System.out.println("我不是一个三角形");
          boo=false;
       }
     }
public void  计算面积()
{  if(boo) 
{  double p=(sideA+sideB+sideC)/2.0;
           double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ;
           System.out.println("面积是:"+area);
        }
      else 
 {  System.out.println("不是一个三角形,不能计算面积");
        }
   } 
   public void 修改三边(double a,double b,double c)
 {  sideA=a;
      sideB=b;
      sideC=c;
      if(a+b>c&&a+c>b&&c+b>a)
{  boo=true;
      }    
    else 
{  boo=false;
      }
   }
}
例子13
import tom.jiafei.Trangle;
class Example4_13 
{  public static void main(String args[])
 {  Trangle trangle=new Trangle(12,3,1);
      trangle.计算面积();
      trangle.修改三边(3,4,5);
      trangle.计算面积();
   }
}
例子14
class Example4_14
{   private int money;
Example4_14() 
{  money=2000;
    }  
private int getMoney()
{  return money;
    } 
public static void main(String args[])
{  Example4_14 exa=new Example4_14();
exa.money=3000;
int m=exa.getMoney();
       System.out.println("money="+m);
    }
}
例子15
class Father 
{  private int money;
   float weight,height;
   String head;
   void speak(String s)
 {  System.out.println(s);
   }
}
class Son extends Father 
{   String hand,foot;
} 
public class Example4_15
{  public static void main(String args[])
{  Son boy;
      boy=new Son();
      boy.weight=1.80f; 
      boy.height=120f;
      boy.head="一个头"; 
      boy.hand="两只手 ";
      boy.foot="两只脚";
      boy.speak("我是儿子");
      System.out.println(boy.hand+boy.foot+boy.head+boy.weight+boy.height);
   }
}    
例子16
class Chengji 
{  float f(float x,float y)
 {   return x*y;
   }
}
class Xiangjia  extends Chengji
{  float f(float x,float y) 
{   return x+y ;
   }
}
public class Example4_16 
{  public static void main(String args[])
 { Xiangjia sum;
     sum=new Xiangjia();
     float c=sum.f(4,6);
     System.out.println(c);
   }
}
例子17
class Area
{  float f(float r ) 
   {  return 3.14159f*r*r;
   }
   float g(float x,float y) 
   {  return x+y;
   }
}
class Circle extends Area 
{   float f(float r)
    { return 3.14159f*2.0f*r;
    }  
}
public class Example4_17
{  public static void main(String args[]) 
   {  Circle yuan;
      yuan=new Circle();
      float length=yuan.f(5.0f);            
      float sum=yuan.g(232.645f,418.567f); 
      System.out.println(length);
      System.out.println(sum);  
   }
}
例子18
class A
{ 
  final double PI=3.1415926;
  public double getArea(final double r)
 {  
     return PI*r*r;
  }
}
public class Example4_18 
{  public static void main(String args[])
 {  A a=new A();
      System.out.println("面积:"+a.getArea(100));
   }
}
例子19
class  类人猿 
{  private int n=100;
   void crySpeak(String s) 
{  System.out.println(s); 
   }  
}
class People extends 类人猿
{  void computer(int a,int b) 
 {  int c=a*b;
      System.out.println(c); 
   }
 void crySpeak(String s) 
 {  System.out.println("**"+s+"**"); 
   }  
}
class Example4_19 
{  public static void main(String args[])
  {  类人猿 monkey=new People();   
      monkey.crySpeak("I love this game");
People people=(People)monkey; 
      people.computer(10,10);
   }
}
例子20
class  动物
{  void cry() 
{
   }  
}
class 狗 extends 动物 
{  void cry()
 {  System.out.println("汪汪....."); 
   }  
}
class 猫 extends 动物 
{  void cry()
 {  System.out.println("喵喵....."); 
   }  
}
class Example4_20 
{  public static void main(String args[])
 {    动物 dongwu;
        dongwu=new 狗();
        dongwu.cry(); 
        dongwu=new 猫();
        dongwu.cry();
   }
}

例子21
abstract class A 
{  abstract  int min(int x,int y);
   int max(int x,int y) 
   {  return x>y?x:y;
   }
}
class B extends A 
{  int min(int x,int y) 
   {  return x<y?x:y;
   }
}
public class Example4_21
{  public static void main(String args[])
   { A a;
     B b=new B();
     int max=b.max(12,34);
     int min=b.min(12,34);
     System.out.println("max="+max+" min="+min);
     a=b;        
     max=a.max(12,34);
     System.out.println("max="+max);
   }
}
例子22
abstract class 图形
{  public abstract double 求面积();
}
class 梯形 extends 图形 
{  double a,b,h;
   梯形(double a,double b,double h)
 {  this.a=a;
this.b=b;
this.h=h;
   }
   public double 求面积()
 {  return((1/2.0)*(a+b)*h);
   }
}
class 圆形 extends 图形 
{  double r;
   圆形(double r)
 {  this.r=r;
   }
   public double 求面积()
 {  return(3.14*r*r);
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -