[关闭]
@Sarah 2015-11-05T17:47:48.000000Z 字数 5892 阅读 929

JAVA 生日 色彩 多态 接口

java笔记
上堂课的例子更新正确版本

1时间/生日

  1. public class Time {
  2. int hour, minute, second;
  3. Time(int hour, int minute, int second) {
  4. this.hour = hour;
  5. this.minute = minute;
  6. this.second = second;
  7. }
  8. Time(int hour, int minute) {
  9. this.hour = hour;
  10. this.minute = minute;
  11. second = 0;
  12. }
  13. Time(int hour) {
  14. this.hour = hour;
  15. minute = 0;
  16. second = 0;
  17. }
  18. public void nextSecond(){
  19. second++;
  20. if(second==60){
  21. second=0;
  22. minute++;
  23. if(minute==60){
  24. minute=0;
  25. hour++;
  26. if(hour==24)
  27. hour=0;
  28. }
  29. }
  30. }
  31. public void nextSecond(long s){
  32. for(int i=0;i<s;i++) nextSecond();
  33. }
  34. @Override//覆盖
  35. public String toString() {
  36. return super.toString()+"[hour=" + hour + ", minute=" + minute + ", second="
  37. + second + "]";
  38. }
  39. }
  1. import java.awt.KeyboardFocusManager;
  2. public class Date {
  3. int year, month, day;
  4. Date(int year, int month, int day) {
  5. this.year = year;
  6. this.month = month;
  7. this.day = day;
  8. }
  9. Date(int year, int month) {
  10. this.year = year;
  11. this.month =month;
  12. day = 1;
  13. }
  14. Date(int year) {
  15. this.year = year;
  16. day = 1;
  17. month = 1;
  18. }
  19. public void nextDay() { // a过了1天以后是什么日期
  20. if(month==2 && day==28){
  21. if (year % 4 == 0 && year % 100 != 0 || year % 400==0){
  22. day++;
  23. }
  24. else{
  25. month=3;
  26. day=1;
  27. }
  28. }
  29. else if(day==29 &&month==2){
  30. month=3;
  31. day=1;
  32. }
  33. else if(day==30){
  34. if(month==4 || month==6 || month==9 || month==11){
  35. month++;
  36. day=1;
  37. }
  38. else {
  39. day++;
  40. }
  41. }else if(day==31){
  42. month++;
  43. if(month==13){
  44. month=1;
  45. year++;
  46. }
  47. day=1;
  48. }
  49. else {
  50. day++;
  51. }
  52. // return new Date(year,month,day);
  53. }
  54. @Override
  55. public boolean equals(Object obj) {
  56. if (this == obj)
  57. return true;
  58. if (obj == null)
  59. return false;
  60. if (getClass() != obj.getClass())
  61. return false;
  62. Date other = (Date) obj;
  63. if (day != other.day||month != other.month||year != other.year)
  64. return false;
  65. return true;
  66. }
  67. @Override
  68. public String toString() {
  69. return "Date [year=" + year + ", month=" + month + ", day=" + day + "]";
  70. }
  71. }
  1. public class Birthday {
  2. Date x;
  3. Time y;
  4. Birthday(Date a, Time b) {
  5. x = a;
  6. y = b;
  7. }
  8. Birthday(Date a) {
  9. x = a;
  10. y = new Time(0, 0, 0);
  11. }
  12. Birthday(int year, int month, int day, int hour, int minute, int second) {
  13. x = new Date(year, month, day);
  14. y = new Time(hour, minute, second);
  15. }
  16. public long birthdayTo(Date x) {// 计算过了多少天
  17. long k = 1;
  18. Date temp=new Date(this.x.year,this.x.month,this.x.day);
  19. while (true) {
  20. temp.nextDay();
  21. //System.out.println(temp);
  22. if (temp.equals(x))
  23. return k;
  24. else
  25. k++;
  26. }
  27. //return 0;
  28. }
  29. @Override
  30. public String toString() {
  31. return "Birthday [x=" + x + ", y=" + y + "]";
  32. }
  33. }
  1. public class Test {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. Date i=new Date(1992, 11, 23);
  5. Date ii=i;
  6. Time m=new Time(20, 45, 22);
  7. System.out.println(i.hashCode());
  8. System.out.println(ii.hashCode());
  9. //System.out.println(m);
  10. //m.nextSecond(60);
  11. //System.out.println(m);
  12. Birthday c=new Birthday(i, m);
  13. Date j=new Date(1993, 11, 23);
  14. Birthday d=new Birthday(j);
  15. System.out.println(c.birthdayTo(j));
  16. System.out.println(c.toString());
  17. }
  18. }

2色彩点案例

坐标上一点,既有坐标,也有颜色

  1. package color;
  2. public class Point {
  3. int x,y;
  4. public 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 color;
  2. public class Test {
  3. public static void main(String[] args) {
  4. Point p3=new Point();
  5. Point p2=new Point(0,0);
  6. Color c=new Color(255,0,0);
  7. ColoredPoint p1=new ColoredPoint(255, 255) ;
  8. Color c1=new Color(p1,255);
  9. int k=c.TestColor(c);
  10. // int k=c1.TestColor(c1);
  11. System.out.println("p1"+p1);
  12. switch(k){
  13. case 1:
  14. System.out.print("white");
  15. break;
  16. case 2:
  17. System.out.print("black");
  18. break;
  19. case 3:
  20. System.out.print("red");
  21. break;
  22. case 4:
  23. System.out.print("green");
  24. break;
  25. case 5:
  26. System.out.print("blue");
  27. break;
  28. case 6:
  29. System.out.print("other color");
  30. break;
  31. }
  32. }
  33. }
  1. package color;
  2. public class Color {
  3. int r, g, b;
  4. public Color() {
  5. }
  6. public Color(int x, int y, int z) {
  7. r =x;
  8. g = y;
  9. b =z;
  10. }
  11. public Color(Point a,int b){
  12. a=new Point(0,0);
  13. b=this.b;
  14. }
  15. int TestColor(Color a) {
  16. if (r == 255 && g == 255 && b == 255)
  17. return 1;// white
  18. else if (r == 0 && g == 0 && b == 0)
  19. return 2;// black
  20. else if (r == 255 && g == 0 && b == 0)
  21. return 3;// red
  22. else if (r == 0 && g == 255 && b == 0)
  23. return 4;// green
  24. else if (r == 0 && g == 0 && b == 255)
  25. return 5;// blue
  26. else
  27. return 6;
  28. }
  29. @Override
  30. public String toString() {
  31. return "[r=" + r + ", g=" + g + ", b=" + b + "]";
  32. }
  33. }
  1. package color;
  2. public class ColoredPoint extends Point {
  3. Color color;
  4. ColoredPoint(int x, int y) {
  5. this(x,y,0,0,0);
  6. }
  7. ColoredPoint(int x, int y, int r,int g,int b) {
  8. color = new Color(r,g,b);
  9. this.x = x;
  10. this.y=y;
  11. }
  12. ColoredPoint(int r,int g,int b) {
  13. this(0,0,r,g,b);
  14. }
  15. @Override
  16. public String toString() {
  17. return "[color=" + color + ", x=" + x + ", y=" + y + "]";
  18. }
  19. }

3interface案例

  1. public class A {
  2. }
  1. public interface B {
  2. int f();
  3. void g();
  4. int i=10;
  5. }
  1. public abstract class C implements B{
  2. void t(){
  3. System.out.println("-----");
  4. }
  5. }
  1. public class D extends C{
  2. @Override
  3. public int f() {
  4. // TODO Auto-generated method stub
  5. return 0;
  6. }
  7. @Override
  8. public void g() {
  9. // TODO Auto-generated method stub
  10. }
  11. }

5不知道什么例子。。。

  1. public class Person {
  2. int age;
  3. static int count=0;
  4. Person(int age){
  5. this.age=age;
  6. count++;
  7. }
  8. public static void main(String[] args) {
  9. Person p1=new Person(10);
  10. System.out.println(p1.count);
  11. Person p2=new Person(20);
  12. System.out.println(p2.count);
  13. }
  14. }

判断两个点是否相等,可以用

  1. p1.equals(p2)

而不是复杂的

  1. if(p.x==x&&p.y==y)

object:
是所有包的父类
定义了所有类应该具有的函数
如 equals()
toString()
(自带的toString()打印的是16进位的哈希码@)


数据结构:


线性表:数组-链表-哈希表
图:

  1. class Node{
  2. int i;
  3. Node next;
  4. }
  5. Node a=new Node();
  6. a.i=10;
  7. Node b=new Node();
  8. b.i=11
  9. a.next=b;

static

static方法:静态
static变量;静态

static方法:

形式 类名.方法名()
不能访问非static变量、方法

一般的方法:

对象.方法()

static变量:

类变量,所有类的对象共用


final

final类:不能继承
final方法:不能覆盖
final变量:常量

  1. class A{
  2. final f(){}
  3. g(){}
  4. }
  5. ///
  6. class{
  7. f(){}
  8. }
  9. ///
  10. class A{
  11. static fanel int i= 10;
  12. }
  13. ///
  14. ///无statac
  15. a=new.A();
  16. a.i

多态

1方法重载引起
2方法覆盖

  1. ///方法覆盖案例
  2. A{
  3. f(){--}
  4. }
  5. B extends A{
  6. f(){1}
  7. }
  8. C extends A{
  9. f(){2}
  10. }
  11. A a=new B();
  12. a.f(); //调用B类1
  13. a= new c();
  14. a.f();//调用C类2

抽象类

不能直接实例化成对象


A:
g() {}
abstract f();

B:
g(){}
f(){}

C:
(){}
f(){}


abstract class A{ }
abstract void f();//继承
void g(){}
A a=new A();
a.g();
a.f()?


abstract class Shape{
public double area();
三角a()
圆b()
矩形c()

接口

interface
纯抽象类,即没有任何具体方法

类f() {}
抽象类1 抽象类2

..............A.............

A a=new A();
a.f();

A不知道调用1 还是2,JAVA不允许


接口f();

子接口f(1) 子接口f(2)

B f()
A af()


class A{}
interface B{}
int f();------------public int f();
void g();---------public void g();
int i=10;--------- public static final int i=10;


absteact
class C implements B{
i=10;
abstract
}

C

D


接口的作用

1多重继承

此处输入图片的描述

2不同的时期代码的公共接口

此处输入图片的描述

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注