面向对象程序设计
封装
继承
多态
在类的声明中,需要定义类的名称,对该类的访问权限和该类的其他类的关系
类的声明格式如下:
【修饰符】class 类名【extends 父类名】implements 接口列表{
}
修饰符:public private abstract final
把类装进包里,在开头声明包的名字即可
package com.wgh
public class Cric{
final float PI =3.1415f;
public void draw(){
System.out.println(“this is a test programming!”)
}
}
继承:
public class Animal{
public Boolean live =true;
public String skin =” “;
pubic void eat(){
System.out.println(“he can eat !”);
}
public void move (){
System.out.println(“he can move”);
}
}
public class Brid extends Aminal{
public String skin=”羽毛”;
public void move(){
System.out.println(“he can fly”); //覆盖了父类的成员方法
}
}
Java中用接口来解决多重继承的问题
1.定义了两个接口
package goodjob;
public interface Caulate {
final float PI = 3.14159f;
float getArea(float r);
float getCircu(float r);
void draw();
}
package goodjob;
public interface GeometryShape {
final float PI = 3.1415926f;
float getArea(float r);
float getCircu(float r);
void draw();
}
2 然后在类中继承两个接口,逗号隔开: public class Test implements Caulate,GeometryShape
/**
*
*/
package goodjob;
/**
* @author Mr.Lv
*
*/
public class Test implements Caulate,GeometryShape{
public float getArea(float r){
float area = Caulate.PI*r*r;
return area;
}
public float getCircu(float r){
float Cir = 2*Caulate.PI*r;
return Cir;
}
public void draw(){
System.out.println("画一个同心圆!");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test = new Test(); // Test test;
float r = 7;
float area=test.getArea(r);
float Cir=test.getCircu(r);
System.out.println("圆的面积为:"+area);
System.out.println("圆的周长为:"+Cir);
System.out.println("圆的面积为:"+test.getArea(8));
test.draw();
}
}
Console:
圆的面积为:153.93791
圆的周长为:43.98226
圆的面积为:201.06177
画一个同心圆!
集合
Colllection
List: ArrayList LinkedList
Set: HashSet TreeSet
Map: HashMap
捕捉异常:
printStackTrace():方法输出异常的详细描述信息
try{
代码块1;
}catch(Exception e){
e.printStackTrace();
}
线程:
开启:start()\
挂起: sleep()/join()/wait()_