@Sarah
2015-11-05T17:47:48.000000Z
字数 5892
阅读 929
java笔记
上堂课的例子更新正确版本
public class Time {
int hour, minute, second;
Time(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
Time(int hour, int minute) {
this.hour = hour;
this.minute = minute;
second = 0;
}
Time(int hour) {
this.hour = hour;
minute = 0;
second = 0;
}
public void nextSecond(){
second++;
if(second==60){
second=0;
minute++;
if(minute==60){
minute=0;
hour++;
if(hour==24)
hour=0;
}
}
}
public void nextSecond(long s){
for(int i=0;i<s;i++) nextSecond();
}
@Override//覆盖
public String toString() {
return super.toString()+"[hour=" + hour + ", minute=" + minute + ", second="
+ second + "]";
}
}
import java.awt.KeyboardFocusManager;
public class Date {
int year, month, day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
Date(int year, int month) {
this.year = year;
this.month =month;
day = 1;
}
Date(int year) {
this.year = year;
day = 1;
month = 1;
}
public void nextDay() { // a过了1天以后是什么日期
if(month==2 && day==28){
if (year % 4 == 0 && year % 100 != 0 || year % 400==0){
day++;
}
else{
month=3;
day=1;
}
}
else if(day==29 &&month==2){
month=3;
day=1;
}
else if(day==30){
if(month==4 || month==6 || month==9 || month==11){
month++;
day=1;
}
else {
day++;
}
}else if(day==31){
month++;
if(month==13){
month=1;
year++;
}
day=1;
}
else {
day++;
}
// return new Date(year,month,day);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Date other = (Date) obj;
if (day != other.day||month != other.month||year != other.year)
return false;
return true;
}
@Override
public String toString() {
return "Date [year=" + year + ", month=" + month + ", day=" + day + "]";
}
}
public class Birthday {
Date x;
Time y;
Birthday(Date a, Time b) {
x = a;
y = b;
}
Birthday(Date a) {
x = a;
y = new Time(0, 0, 0);
}
Birthday(int year, int month, int day, int hour, int minute, int second) {
x = new Date(year, month, day);
y = new Time(hour, minute, second);
}
public long birthdayTo(Date x) {// 计算过了多少天
long k = 1;
Date temp=new Date(this.x.year,this.x.month,this.x.day);
while (true) {
temp.nextDay();
//System.out.println(temp);
if (temp.equals(x))
return k;
else
k++;
}
//return 0;
}
@Override
public String toString() {
return "Birthday [x=" + x + ", y=" + y + "]";
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Date i=new Date(1992, 11, 23);
Date ii=i;
Time m=new Time(20, 45, 22);
System.out.println(i.hashCode());
System.out.println(ii.hashCode());
//System.out.println(m);
//m.nextSecond(60);
//System.out.println(m);
Birthday c=new Birthday(i, m);
Date j=new Date(1993, 11, 23);
Birthday d=new Birthday(j);
System.out.println(c.birthdayTo(j));
System.out.println(c.toString());
}
}
坐标上一点,既有坐标,也有颜色
package color;
public class Point {
int x,y;
public Point(){
this(0,0);
}
public Point(int bb){
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
Point getPoint(){
Point p=new Point();
p.setX(-x);
p.setY(-y);
return p;
}
public void setX(long xx){
x=(int)xx;
}
void setX(int xx){
x=xx;
}
void setY(int yy){
y=yy;
}
Point getApoint(){ //新函数 返回值是point
Point q=new Point(-x,-y);
return q;
}
public double distance(Point p2){ //point是原来的点坐标,p2是另一点的坐标
double d=0;
d=Math.sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y));
return d;
}
public boolean equals(Point p3){
if (p3.x==x && p3.y==y) {
return true;
}
else {
return false;
}
}
public String toString(){
return "["+x+","+y+"]";
}
public int getX(){
return x;
}
public int getY() {
return y;
}
}
package color;
public class Test {
public static void main(String[] args) {
Point p3=new Point();
Point p2=new Point(0,0);
Color c=new Color(255,0,0);
ColoredPoint p1=new ColoredPoint(255, 255) ;
Color c1=new Color(p1,255);
int k=c.TestColor(c);
// int k=c1.TestColor(c1);
System.out.println("p1"+p1);
switch(k){
case 1:
System.out.print("white");
break;
case 2:
System.out.print("black");
break;
case 3:
System.out.print("red");
break;
case 4:
System.out.print("green");
break;
case 5:
System.out.print("blue");
break;
case 6:
System.out.print("other color");
break;
}
}
}
package color;
public class Color {
int r, g, b;
public Color() {
}
public Color(int x, int y, int z) {
r =x;
g = y;
b =z;
}
public Color(Point a,int b){
a=new Point(0,0);
b=this.b;
}
int TestColor(Color a) {
if (r == 255 && g == 255 && b == 255)
return 1;// white
else if (r == 0 && g == 0 && b == 0)
return 2;// black
else if (r == 255 && g == 0 && b == 0)
return 3;// red
else if (r == 0 && g == 255 && b == 0)
return 4;// green
else if (r == 0 && g == 0 && b == 255)
return 5;// blue
else
return 6;
}
@Override
public String toString() {
return "[r=" + r + ", g=" + g + ", b=" + b + "]";
}
}
package color;
public class ColoredPoint extends Point {
Color color;
ColoredPoint(int x, int y) {
this(x,y,0,0,0);
}
ColoredPoint(int x, int y, int r,int g,int b) {
color = new Color(r,g,b);
this.x = x;
this.y=y;
}
ColoredPoint(int r,int g,int b) {
this(0,0,r,g,b);
}
@Override
public String toString() {
return "[color=" + color + ", x=" + x + ", y=" + y + "]";
}
}
public class A {
}
public interface B {
int f();
void g();
int i=10;
}
public abstract class C implements B{
void t(){
System.out.println("-----");
}
}
public class D extends C{
@Override
public int f() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void g() {
// TODO Auto-generated method stub
}
}
public class Person {
int age;
static int count=0;
Person(int age){
this.age=age;
count++;
}
public static void main(String[] args) {
Person p1=new Person(10);
System.out.println(p1.count);
Person p2=new Person(20);
System.out.println(p2.count);
}
}
判断两个点是否相等,可以用
p1.equals(p2)
而不是复杂的
if(p.x==x&&p.y==y)
object:
是所有包的父类
定义了所有类应该具有的函数
如 equals()
toString()
(自带的toString()打印的是16进位的哈希码@)
树
线性表:数组-链表-哈希表
图:
class Node{
int i;
Node next;
}
Node a=new Node();
a.i=10;
Node b=new Node();
b.i=11
a.next=b;
static方法:静态
static变量;静态
形式 类名.方法名()
不能访问非static变量、方法
对象.方法()
类变量,所有类的对象共用
final类:不能继承
final方法:不能覆盖
final变量:常量
class A{
final f(){}
g(){}
}
///
class{
f(){}
}
///
class A{
static fanel int i= 10;
}
///
///无statac
a=new.A();
a.i
1方法重载引起
2方法覆盖
///方法覆盖案例
A{
f(){--}
}
B extends A{
f(){1}
}
C extends A{
f(){2}
}
A a=new B();
a.f(); //调用B类1
a= new c();
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