updating Inheritance

Inheritance Syntax

When you using the keyword extends followed by the name of the base class,when you do this,you automatically get all the fields and methods in the base class.

 

When you created an object of the derived class,it contains within it a subobject of the base class.This subobject is the same as if you had created an object of the base class by itself,it’s just that from the outside,the subobject of the base class is the wrapped within the derived-class object.

e.g

class Art {

    Art() {

       System.out.println("Art's constructor");

    }

}

class Drawing extends Art {

   Drawing() {

      System.out.println("Drawing's constructor");

   }

}

public class Cartoon extends Drawing{

   Cartoon() {

   System.out.println("Cartoon's constructor");

   }

   public static void main(String[] args) {

      Cartoon cart=new Cartoon();

     

   }

}

无论写不写,子类总是默认首先执行父类无参的构造

The call to the base-class constructor must be the first thing you do in the derived-class constructor.

The Final keyword,with a primitive,final makes the value a constant.but with a object reference,final makes the reference a constant(and the object itself could be changed)

9/23/2009 2:18:34 PM

今天上课的例题,map加反射,完全做不出来

import java.lang.reflect.*;

import java.util.Map;

import java.util.HashMap;

import java.util.Set;

public class TestFill {

      public static Object getInstance (String name,Map<String,Object> map) throws Exception {

         Class c=Class.forName(name);

         Object obj=c.newInstance();

         Set<String> set=map.keySet();

         for(String key:set) {

            String mName="set"+key.substring(0, 1).toUpperCase()+key.substring(1);         

            Field f=c.getDeclaredField(key);

            Method m=c.getMethod(mName,f.getType());

            m.invoke(obj, map.get(key));

         }

         return obj;

      }

   public static void main(String[] args)  throws Exception {

      Map<String,Object> map=new HashMap<String,Object>();

      map.put("name","zhang");

      map.put("age", 23);

      map.put("id",3);

      Object obj=TestFill.getInstance("date0923.Student", map);

      System.out.println(obj);

   }

 

}

 

public class Student {

      private int age;

      private String name;

      private int id;

      public Student() {

         super();

         System.out.println("Stu1");

      }

      public Student(int age,String name,int id) {

         super();

         System.out.println("Stu2");

         this.setAge(age);

         this.setName(name);

         this.setId(id);

      }

      @Override

      public String toString() {

         return name+","+id+","+age;

      }

      public int getAge() {

         return age;

      }

      public void setAge(int age) {

         this.age = age;

      }

      public String getName() {

         return name;

      }

      public void setName(String name) {

         this.name = name;

      }

      public int getId() {

         return id;

      }

      public void setId(int id) {

         this.id = id;

      } 

}

三个方法的重写,

@Override

equals,hashCode,toString

String 背下来,String的各项办法

List/Map用法

Integer.parseInt();

Double.parseDouble();

Try/catch 或者throws异常,学会看异常

多看API

Producing the right behavior

Circle

reference

Shape

draw()               erase()

 

Circle

draw()               erase()

 

 

Square

draw()               erase()

 

 

Triangle

draw()               erase()

 

 

     

 

Shape s=new Circle();   Circle is a shape by inheritance.

sub actually contains two fields called field,its own and it gets from Super.v

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值