复习:
面向对象
类和对象
class 类名{
属性(成员变量)
方法(4)
}
重载()
构造方法
this. this()
0,面向对象三大特性:继承,封装,多态
1,继承:xx是一种xx
public class Cat extends Animal
子类 父类,基类,超类
作用:简化代码(减少代码冗余)
构造方法:父类对象先与子类对象创建
super():调用父类构造函数
重写:当父类方法不能满足子类需求 方法名相同,参数也得相同,子类重写方法访问修饰符不能比父类更严格
equals hashcode
2,访问修饰符 本类 同包 继承 无限制
public --公有 T T T T
protected --受保护的 T T T
默认 T T
private --私有的 T
3,static -- 静态的,全局唯一 Math
静态方法中不能出现非静态元素
4,final 不可变的 -- 类:不能有子类
属性:必须赋初值,若不赋初值,需要在构造函数中赋初值,一旦赋值, 值不能被改变
方法:不能被重写
局部变量:可以不赋初值。一旦赋值,值不能被改变
Animal.java |
public class Animal { public Animal(){ System.out.println("animal"); } public void eat(){ System.out.println("吃饭"); } protected void sleep(){ System.out.println("睡觉"); } void say(){ System.out.println("说话"); } private void run(){ System.out.println("跑"); } } |
Cat |
public class Cat extends Animal{ public Cat(){ //super() System.out.println("cat"); } public static void main(String[] args){ Cat c = new Cat(); } } |
Demo |
public class Demo { public static void main(String[] args){ /* Man m = new Man(); System.out.println(m.sex); m.eat();*/ // System.out.println(Math.round(3.54)); // [0,1) System.out.println((int)(Math.random()*33+1)); } } |
JiSuanQi |
public class JiSuanQi { public int add(int a,int b){ return a+b; } public int mulAgdd(int a){ int sum = 0; for(int i=1;i<=a;i++){ sum+=i; } return sum; } public double cf(int a,int b){ return Math.pow(a,b); } } |
Man.java |
public class Man { public static String sex = "男"; public static void eat(){ System.out.println("chifan"); } public void s(){ } } |
Mouse.java |
public class Mouse extends Animal{ public void eat(){ super.eat(); System.out.println("吃大米"); } public void sleep(){ System.out.println("睡觉"); } } |
MyTime.java |
public class MyTime { //时(hour),分(minute),秒(second) public int hour; public int minute; public int second; public MyTime(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } public void addSecond(int sec){ this.second += sec; if(this.second>=60){ addMinute(1); this.second -= 60; } } public void addMinute(int min){ this.minute += min; if(this.minute>=60){ addHour(1); this.minute -= 60; } } public void addHour(int hou){ this.hour += hou; if(this.hour>=24){ this.hour -= 24; } } public void subSecond(int sec){ } public void subMinute(int min){ } public void subHour(int hou){ } } |
Person.java |
public class Person { public String name; public int age; public Person( String name,int age){ this.name = name; this.age = age; } public void display(){ System.out.println(name+" "+age); } public static void main(String[] args){ Person p = new Person("zs",18); p.display(); } } |
Pig.java |
public class Pig{ public String name;
public int hashCode(){ return name.hashCode(); } public boolean equals(Object obj) {
return (this.name == ((Pig)obj).name); } } |
Test.java |
public class Test { public static void main(String[] args){ //Cat c = new Cat(); //Mouse m = new Mouse(); //m.eat(); /*Pig p = new Pig(); Pig p1 = p; System.out.println(p.hashCode()); System.out.println(p1.hashCode()); System.out.println(p.equals(p1));*/ /*Pig p = new Pig(); p.name = "pq"; Pig p1 = new Pig(); p1.name = "pq"; System.out.println(p.equals(p1));*/ /*String s = new String("asd"); String s1 = new String("asd"); System.out.println(s.hashCode()); System.out.println(s1.hashCode());*/ //Cat c = new Cat(); } } |
WuMingFen.java |
public class WuMingFen { //面码:String theMa 粉的份量(两):int quantity //是否带汤:boolean likeSoup public String theMa; public int quantity; public boolean likeSoup;
public WuMingFen(String theMa, int quantity, boolean likeSoup) { this.theMa = theMa; this.quantity = quantity; this.likeSoup = likeSoup; } public WuMingFen(String theMa, int quantity) { this.theMa = theMa; this.quantity = quantity; this.likeSoup = true; } public WuMingFen() { } public void check(){ System.out.println(theMa); System.out.println(quantity); System.out.println(likeSoup); } public static void main(String[] args){ WuMingFen wmf = new WuMingFen("刀削面",2); wmf.check(); } }
|
ATM |
public class ATM { private String[][] count = {{"1001","123456","500"},{"1002","123456","1500"}};
public boolean login(String name,String password){ //for count return false; } public void getBalance(String name){
} public void saveMoney(String name,int money){
} public void getMoney(String name,int money){
} public void tranMoney(String name,String other ,int money){ } } |