Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48511
  • 博文数量: 31
  • 博客积分: 228
  • 博客等级: 二等列兵
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-09 23:57
文章分类

全部博文(31)

文章存档

2012年(31)

我的朋友
最近访客

分类:

2012-04-13 00:08:08

原文地址:Java 基础类和对象 作者:luozhiyong131


点击(此处)折叠或打开

  1. class BirthDate {
  2.     private int day;
  3.     private int month;
  4.     private int year;
  5.     
  6.     
  7.     BirthDate(int d, int m, int y) {
  8.         day = d;
  9.         month = m;
  10.         year = y;
  11.     }
  12.     
  13.     public void setDay(int d) {
  14.         day = d;
  15.       }
  16.       
  17.     public void setMonth(int m) {
  18.         month = m;
  19.     }
  20.     
  21.     public void setYear(int y) {
  22.         year = y;
  23.     }
  24.     
  25.     public int getDay() {
  26.         return day;
  27.     }
  28.     
  29.     public int getMonth() {
  30.         return month;
  31.     }
  32.     
  33.     public int getYear() {
  34.         return year;
  35.     }
  36.     
  37.     public void display() {
  38.         System.out.println
  39.         (day + " - " + month + " - " + year);
  40.     }
  41. }


  42. public class Test{
  43.     public static void main(String args[]){
  44.         Test test = new Test();
  45.         int date = 9;
  46.         BirthDate d1= new BirthDate(7,7,1970);
  47.         BirthDate d2= new BirthDate(1,1,2000);
  48.         test.change1(date);
  49.         test.change2(d1);
  50.         test.change3(d2);
  51.         System.out.println("date=" + date);
  52.         d1.display();
  53.         d2.display();
  54.     }
  55.     
  56.     public void change1(int i){
  57.         i = 1234;
  58.     }
  59.     
  60.     public void change2(BirthDate b) {
  61.         b = new BirthDate(22,2,2004);
  62.     }
  63.     
  64.     public void change3(BirthDate b) {
  65.         b.setDay(22);
  66.     }
  67. }


点击(此处)折叠或打开

  1. class Point {
  2.     double x, y, z;
  3.     
  4.     Point(double _x, double _y, double _z) {
  5.         x = _x;
  6.         y = _y;
  7.         z = _z;
  8.     }
  9.     
  10.     void setX(double _x) {
  11.         x = _x;
  12.     }
  13.     
  14.     double getDistance(Point p) {
  15.         return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z)*(z - p.z);
  16.     }
  17.     
  18. }

  19. public class TestPoint {
  20.     public static void main(String[] args) {
  21.         Point p = new Point(1.0, 2.0, 3.0);
  22.         Point p1 = new Point(0.0, 0.0, 0.0);
  23.         System.out.println(p.getDistance(p1));
  24.         
  25.         p.setX(5.0);
  26.         System.out.println(p.getDistance(new Point(1.0, 1.0, 1.0)));
  27.     }
  28. }
课件: Java 基础类和对象.pdf   
阅读(337) | 评论(0) | 转发(0) |
0

上一篇:Java 语句

下一篇:Java 方法重载

给主人留下些什么吧!~~