一、目的
- 学习代码块的作用和形式及如何使用。
- 掌握内部类的使用
- 学习继承和多态的使用
- demo小练习
二、技术及其使用
1.代码块
{
age=20;
System.out.println("代码块 age="+age);
}
//静态代码块
static {
// age=20;
System.out.println("静态代码块");
}
2.内部类的使用
//定义一个内部类 用于管理相对布局的具体布局属性
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开发");
Person2 w2=new woker("rose",36,7600,"Android开发");
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();
}
}
}
}
四、心得体会
JAVA对于我来说还是有点难啊,听的时候还好虽然也有很多地方不明白,但自己写的时候才是最糟糕的,感觉什么都不知道,无从下手。看东哥写一遍后再写的时候还是会卡顿,但第二遍的时候就好一些了,所以还是要多练。