一、目的
- 學(xué)習(xí)代碼塊的作用和形式及如何使用演顾。
- 掌握內(nèi)部類的使用
- 學(xué)習(xí)繼承和多態(tài)的使用
- demo小練習(xí)
二供搀、技術(shù)及其使用
1.代碼塊
{
age=20;
System.out.println("代碼塊 age="+age);
}
//靜態(tài)代碼塊
static {
// age=20;
System.out.println("靜態(tài)代碼塊");
}
2.內(nèi)部類的使用
//定義一個(gè)內(nèi)部類 用于管理相對(duì)布局的具體布局屬性
public class LayoutParams{
float leftMergin;
float topMergin;
float rightMergin;
float bottomMergin;
public LayoutParams(float leftMergin,float topMergin,float rightMergin,float bottomMergin){
this.leftMergin=leftMergin;
this.topMergin=topMergin;
this.rightMergin=rightMergin;
this.bottomMergin=bottomMergin;
}
}
3.繼承
(1)父類
class Person1{
}
(2)子類
class Student extends Person1{
}
三隅居、demo代碼
public class Person2 {
protected String name;
protected int age;
public Person2(String name,int age){
this.name=name;
this.age=age;
}
public void walk(){}
public void eat(){}
}
class Gwy extends Person2{
int salary;
int count;
public Gwy(String name,int age,int salary,int count){
super(name,age);
this.salary=salary;
this.count=count;
}
@Override
public void walk(){
System.out.println("閑庭信步");
}
@Override
public void eat(){
System.out.println("大吃大喝");
}
public void gshow() {
System.out.println( "Gwy{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", count=" + count +
'}');
}
}
class woker extends Person2{
int salary;
String tec;
public woker(String name, int age,int salary,String tec) {
super(name, age);
this.salary=salary;
this.tec=tec;
}
@Override
public void walk(){
System.out.println("快步走");
}
@Override
public void eat(){
System.out.println("省吃儉用");
}
public void wshow(){
System.out.println("woker{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", tec='" + tec + '\'' +
'}');
}
}
class test{
public static void main(String[] args){
ArrayList<Person2> persons=new ArrayList<>();
Person2 g1=new Gwy("小王",24,12500,16);
Person2 g2=new Gwy("小江",23,11000,10);
persons.add(g1);
persons.add(g2);
Person2 w1=new woker("jack",34,8500,"IOS開發(fā)");
Person2 w2=new woker("rose",36,7600,"Android開發(fā)");
persons.add(w1);
persons.add(w1);
for (Person2 p:persons){
if (p instanceof Gwy) {
Gwy g = (Gwy) p;
g.gshow();
g.walk();
g.eat();
}else {
woker w = (woker) p;
w.wshow();
w.walk();
w.eat();
}
}
}
}
四、心得體會(huì)
JAVA對(duì)于我來說還是有點(diǎn)難啊趁曼,聽的時(shí)候還好雖然也有很多地方不明白军浆,但自己寫的時(shí)候才是最糟糕的,感覺什么都不知道挡闰,無從下手£蹋看東哥寫一遍后再寫的時(shí)候還是會(huì)卡頓摄悯,但第二遍的時(shí)候就好一些了,所以還是要多練愧捕。