[关闭]
@Sarah 2015-11-05T17:43:23.000000Z 字数 7499 阅读 1223

Java 构造函数 equals() 面向对象 圆


java笔记
面向对象 OOP
java c++ OC

类:模板
对象:实现

类似电脑包括硬盘/cpu/软盘,要制作硬盘有个模板(类),不同的硬盘具体不一样(对象)

主程序→ 发消息→ 【对象】→ 发消息→ 【对象】

发消息

【对象】

  1. class 名称{第一个字母大写 多个英文词
  2. 成员 【变量定义:多个】
  3. 【函数定义:多个】
  4. }
 这里的变量之后还要实例化 
 每个新的对象都包括变量 和函数
  1. 变量:类型 名称;
  2. 类型 名称=初始;
  3. 函数:返回值类型 名称 ([参数]){
  4. 代码
  5. }

大案例:
定义二位坐标点类point

  1. class point{
  2. int x,y;
  3. //改变点的数值
  4. setX(int x){}
  5. setY(int y){}
  6. //求两点距离
  7. distance(){}
  8. //求关于原点对称点
  9. getApoint(){}
  10. //两点是否相等
  11. equals(){}

piont p ←发消息← p赋值

构造函数:名称=类名,无返回说明
函数重载:同名的两个函数
封装:内部的数据、代码

override是重写(覆盖)了一个方法,以实现不同的功能。一般是用于子类在继承父类时,重写(重新实现)父类中的方法。
重写(覆盖)的规则:
1、重写方法的参数列表必须完全与被重写的方法的相同,否则不能称其为重写而是重载.
2、重写方法的访问修饰符一定要大于被重写方法的访问修饰符(public>protected>default>private)。
3、重写的方法的返回值必须和被重写的方法的返回一致;
4、重写的方法所抛出的异常必须和被重写方法的所抛出的异常一致,或者是其子类;
5、被重写的方法不能为private,否则在其子类中只是新定义了一个方法,并没有对其进行重写。
6、静态方法不能被重写为非静态的方法(会编译出错)。

overload是重载,一般是用于在一个类内实现若干重载的方法,这些方法的名称相同而参数形式不同。
重载的规则:
1、在使用重载时只能通过相同的方法名、不同的参数形式实现。不同的参数类型可以是不同的参数类型,不同的参数个数,不同的参数顺序(参数类型必须不一样);
2、不能通过访问权限、返回类型、抛出的异常进行重载;
3、方法的异常类型和数目不会对重载造成影响;

多态的概念比较复杂,有多种意义的多态,一个有趣但不严谨的说法是:继承是子类使用父类的方法,而多态则是父类使用子类的方法。
一般,我们使用多态是为了避免在父类里大量重载引起代码臃肿且难于维护。

构造函数是一个计算机术语,是一种特殊的方法,主要用来在创建对象时初始化对象,构造函数的命名必须和类名完全相同,而一般方法则不能和类名相同。特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类型的不同来区分它们 即构造函数的重载。一个班级有学生,有的是TA有的是拿奖学金的,为了能够把每一个学生类型匹配,需要构造函数来建立不同的参数把他们对应起来。
1构造方法必须与所在的类有相同的名字
2构造方法没有返回类型,连void也没有
3构造方法的调用是创建一个对象时使用new进行的,构造方法的作用是初始化对象。

构造方法(constructor )是一种特殊的方法
• 用来初始化(new)该类的一个新的对象
• 构造方法和类名同名,而且不写返回数据类型。

  1. Person( String n, int a ){
  2. name = n;
  3. age = a;
  4. }

一般情况下,类都有一个至多个构造方法
• 如果没有定义任何构造方法,系统会自动产生一个构造方法,称为默认构造方法(default constructor)。
• 默认构造方法不带参数,并且方法体为空。

equals() 判断是否相等
toString() 转换成字符串输出

  1. public class Point {
  2. int x,y;
  3. Point(){
  4. }
  5. Point(int bb){
  6. }
  7. Point(int xx,int yy){
  8. x=xx;
  9. y=yy;
  10. }
  11. void setX(long xx){
  12. x=(int)xx;
  13. }
  14. void setX(int xx){
  15. x=xx;
  16. }
  17. void setY(int yy){
  18. y=yy;
  19. }
  20. Point getApoint(){
  21. Point q=new Point(-x,-y);
  22. return q;
  23. }
  24. double distance(Point p2){
  25. double d=0;
  26. d=Math.sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y));
  27. return d;
  28. }
  29. boolean equals(Point p3){
  30. if (p3.x==x && p3.y==y) {
  31. return true;
  32. }
  33. else {
  34. return false;
  35. }
  36. }
  37. public String toString(){
  38. return "("+x+","+y+")";
  39. }
  40. }
  41. //不要再这里输出打印任何东西
  1. public class Main {
  2. public static void main(String[] args) {
  3. Point p;
  4. p=new Point(10);
  5. // System.out.println("x="+p.x+"\ty="+p.y);
  6. p.setX(100l);
  7. // System.out.println("x="+p.x+"\ty="+p.y);
  8. p.setY(-100);
  9. System.out.println("p:"+p.toString());
  10. Point t=p.getApoint();
  11. System.out.println("ttttt:"+t);
  12. Point q=t.getApoint();
  13. System.out.println("q.x="+p.x+"\tq.y="+p.y);
  14. System.out.println( q.distance( t) );
  15. System.out.println(p.equals(t));
  16. System.out.println(p.equals(q));
  17. }
  18. }

关于圆的关系
我写的有错误的代码

  1. package Circle;
  2. import java.awt.Point;
  3. public class Circle {
  4. int x, y;
  5. int r;
  6. Point center;
  7. int radius;
  8. Circle() {
  9. center = new Point();
  10. radius = 1;
  11. }
  12. Circle(int x, int y) {
  13. radius = 0;
  14. }
  15. Circle(int xx, int yy, int r) {
  16. center = new Point(xx, yy);
  17. radius = r;
  18. }
  19. Circle(Point p, int r) {
  20. center = p;
  21. radius = r;
  22. }
  23. Point setCenter(Point p1) { // 设置圆心坐标
  24. x = p1.x;
  25. y = p1.y;
  26. return p1;
  27. }
  28. void setR(int rr) { // 设置圆的半径
  29. radius = rr;
  30. }
  31. double getArea(int r) { // 求面积
  32. return radius * radius * 3.1415926;
  33. }
  34. int relation(Circle c) {
  35. double distance = Math.sqrt((x - c.x) * (x - c.x) + (y - c.y)
  36. * (y - c.y));
  37. int k = 0;
  38. if (distance > r + c.r) {
  39. return k = 0;
  40. } else if (distance == r + c.r) {
  41. return k = 1;
  42. } else if (r - c.r < distance && distance < r + c.r) {
  43. return k = 2;
  44. } else if (distance == r - c.r) {
  45. return k = 3;
  46. } else {
  47. return k = 4;
  48. }
  49. }
  50. boolean equals(Circle c2) { // 判断两个圆是否相等
  51. if (c2.x == x && c2.y == y && c2.r == r) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. public String toString() {
  58. return "(" + x + "," + y + "," + r + ")";
  59. }
  60. }
  1. package Circle;
  2. import java.awt.Point;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Point p1 = new Point();
  6. p1.x=5;
  7. p1.y=4;
  8. Point p2 = new Point();
  9. p2.x=6;
  10. p2.y=7;
  11. int r1 = 5;
  12. Circle c1 = new Circle(p1, 8);
  13. Circle c2 = new Circle();
  14. c2.x=5;
  15. c2.y=6;
  16. c2.r=9;
  17. Circle c3 = new Circle();
  18. c3.x=5;
  19. c3.y=6;
  20. c3.r=7;
  21. // p1.setCenter(p2); 有错
  22. // System.out.println("P1:"+p1);
  23. // c3.setR(9); 有错
  24. // System.out.println("C3:"+c3.toString());
  25. // System.out.println(c2.getArea(r1)); 有错
  26. System.out.println(c2.relation(c1));
  27. System.out.println(c1.equals(c2));
  28. System.out.println("C2:"+c2.toString());
  29. }
  30. }

我改了一次以后的代码

  1. package Circle;
  2. import java.awt.Point;
  3. public class Circle {
  4. int x, y;
  5. Point center;
  6. int radius;
  7. Circle() {
  8. }
  9. Circle(int xx, int yy) {
  10. x=xx;
  11. y=yy;
  12. radius = 0;
  13. }
  14. Circle(int xxx, int yyy, int rr) {
  15. x=xxx;
  16. y=yyy;
  17. radius = rr;
  18. }
  19. Circle(Point p, int rrr) {
  20. x = p.x;
  21. y=p.y;
  22. radius = rrr;
  23. }
  24. Point setCenter(Point p1) { // 设置圆心坐标
  25. x = p1.x;
  26. y = p1.y;
  27. return p1;
  28. }
  29. void setR(int rr) { // 设置圆的半径
  30. radius = rr;
  31. }
  32. double getArea(int radius) { // 求面积
  33. return radius * radius * 3.1415926;
  34. }
  35. int relation(Circle c) { //求两圆关系
  36. double distance = Math.sqrt((x - c.x) * (x - c.x) + (y - c.y)
  37. * (y - c.y));
  38. int k = 0;
  39. if (distance > radius + c.radius) {
  40. return k = 0;
  41. } else if (distance == radius + c.radius) {
  42. return k = 1;
  43. } else if (radius - c.radius < distance && distance < radius + c.radius) {
  44. return k = 2;
  45. } else if (distance ==radius - c.radius) {
  46. return k = 3;
  47. } else {
  48. return k = 4;
  49. }
  50. }
  51. boolean equals(Circle c2) { // 判断两个圆是否相等
  52. if (c2.x == x && c2.y == y && c2.radius == radius) {
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. }
  58. public String toString() {
  59. return "(" + x + "," + y + "," + radius + ")";
  60. }
  61. }
  1. package Circle;
  2. import java.awt.Point;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Point p1 = new Point();
  6. p1.x=5;
  7. p1.y=4;
  8. Point p2 = new Point();
  9. p2.x=6;
  10. p2.y=7;
  11. int radius = 5;
  12. Circle c1 = new Circle(p1, 8);
  13. Circle c2 = new Circle();
  14. c2.x=5;
  15. c2.y=6;
  16. c2.radius=9;
  17. Circle c3 = new Circle();
  18. c3.x=4;
  19. c3.y=8;
  20. c3.radius=7;
  21. // p1.setCenter(p2);
  22. // System.out.println("P1:"+p1);
  23. c3.setR(5);
  24. System.out.println("C3: "+c3.toString());
  25. System.out.println("圆的面积是: "+c2.getArea(c2.radius));
  26. System.out.println("两个圆的关系: "+c2.relation(c1));
  27. System.out.println("两圆是否相等: "+c1.equals(c2));
  28. System.out.println("C2: "+c2.toString());
  29. }
  30. }

更改以后的答案

  1. package Circle;
  2. public class Circle {
  3. Point center;
  4. int radius;
  5. //不要设置乱七八糟的x y变量了!因为圆里的center引用的点对象包括了x y,所以只要设点对象就好,之后召唤的时候用center.x/center.y即可
  6. Circle() {
  7. center=new Point(0,0);
  8. }
  9. Circle(int xx, int yy) {
  10. center=new Point(xx,yy);//这里需要new出一个新的内存空间,否则会没地方存储数据(null)
  11. radius = 0;
  12. }
  13. Circle(int xx, int yy, int rr) {
  14. center=new Point(xx,yy);
  15. radius = rr;
  16. }
  17. Circle(Point p, int rrr) {
  18. center=p;
  19. radius = rrr;
  20. }
  21. void setCenter(Point p1) { // 设置圆心坐标
  22. center=p1;
  23. }
  24. void setR(int rr) { // 设置圆的半径
  25. radius = rr;
  26. }
  27. double getArea() { // 求面积
  28. return radius * radius * 3.1415926;
  29. }
  30. int relation(Circle c) { //求两圆关系
  31. double distance = center.distance(c.center); //重要 ※ 记住
  32. if (equals(c))
  33. return 5;
  34. else if (distance > radius + c.radius) {
  35. return 0;
  36. } else if (distance == radius + c.radius) {
  37. return 1;
  38. } else if (radius - c.radius < distance && distance < radius + c.radius) {
  39. return 2;
  40. } else if (distance ==radius - c.radius) {
  41. return 3;
  42. } else {
  43. return 4;
  44. }
  45. }
  46. boolean equals(Circle c2) { // 判断两个圆是否相等
  47. if (c2.center.x == center.x && c2.center.y == center.y && c2.radius == radius) {
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. public String toString() {
  54. return "{" + center + "," + radius + "}";
  55. }
  56. }
  1. package Circle;
  2. public class Point {
  3. int x,y;
  4. private Point(){
  5. this(0,0);
  6. }
  7. public Point(int bb){
  8. }
  9. public Point(int x,int y){
  10. this.x=x;
  11. this.y=y;
  12. }
  13. Point getPoint(){
  14. Point p=new Point();
  15. p.setX(-x);
  16. p.setY(-y);
  17. return p;
  18. }
  19. public void setX(long xx){
  20. x=(int)xx;
  21. }
  22. void setX(int xx){
  23. x=xx;
  24. }
  25. void setY(int yy){
  26. y=yy;
  27. }
  28. Point getApoint(){ //新函数 返回值是point
  29. Point q=new Point(-x,-y);
  30. return q;
  31. }
  32. public double distance(Point p2){ //point是原来的点坐标,p2是另一点的坐标
  33. double d=0;
  34. d=Math.sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y));
  35. return d;
  36. }
  37. public boolean equals(Point p3){
  38. if (p3.x==x && p3.y==y) {
  39. return true;
  40. }
  41. else {
  42. return false;
  43. }
  44. }
  45. public String toString(){
  46. return "["+x+","+y+"]";
  47. }
  48. public int getX(){
  49. return x;
  50. }
  51. public int getY() {
  52. return y;
  53. }
  54. }
  1. package Circle;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Point p1 = new Point(0, 4);//这里new出了新空间
  5. p1.x = 5;
  6. p1.y = 4;
  7. Point p2 = new Point(6, 7);//new出空间存放数据
  8. // p2.x=6;
  9. // p2.y=7;
  10. int radius = 5;
  11. Circle c1 = new Circle(p1, 8);
  12. Circle c2 = new Circle();
  13. System.out.println(c2.center);
  14. System.out.println(c2.center.x);
  15. c2.setCenter(new Point(5, 6));
  16. c2.radius = 9;
  17. Circle c3 = new Circle(10, 10);
  18. c3.radius = 7;
  19. c3.setCenter(p2);
  20. System.out.println("P1:" + p1);//是圆里的这点
  21. c3.setR(5);
  22. System.out.println("C3: " + c3.toString());
  23. System.out.println("圆的面积是: " + c2.getArea());
  24. System.out.println("两圆是否相等: " + c1.equals(c2));
  25. System.out.println("C2: " + c2.toString());
  26. System.out.println("C1: " + c1.toString());
  27. int r = c1.relation(c2);
  28. System.out.print("c1,c2两个圆的关系: ");
  29. switch (r) { //用switch语句打印,能针对不同情况打印出更多的内容
  30. case 0:
  31. System.out.println("相离");
  32. break;
  33. }
  34. }
  35. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注