A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马刘涛 中级黑马   /  2012-7-26 17:36  /  3356 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.itcast.test1;

  2. public class AbstractConstructorDemo {
  3.         /**
  4.          * 在父类中(这里就是你的抽象类)只写了有参数的构造函数,
  5.          * 在子类继承是就必须写一个构造函数来调用父类的构造函数。
  6.          */
  7.         public static void main(String[] args) {
  8.                 new Student1();
  9.                 //new Person(); // 抽象类可以有构造函数,但是不能实例化。
  10.         }

  11. }
  12. abstract class Person { //定义一个抽象类,必须被继承  
  13.         Person() {
  14.                 System.out.println("抽象类无参数构造函数已运行");
  15.         }
  16.     Person(int i) {
  17.             System.out.println(i);
  18.             System.out.println("抽象类有参数构造函数已运行");
  19.     }
  20. }

  21. class Student1 extends Person {
  22.         /*private int i = 0;
  23.         Student1(int i) {
  24.             super(i);//必须显示的调用父类构造方法
  25.             System.out.println(this.i);
  26.             System.out.println("学生类构造函数已运行");        
  27.     }
  28.     */
  29.         public Student1() {               
  30.                 //super(0); // 可不写
  31.                 System.out.println("学生类构造函数已运行");
  32.         }
  33. }

复制代码

5 个回复

倒序浏览
本帖最后由 王渠 于 2012-7-26 19:23 编辑

o(∩_∩)o 哈哈,满足你哦,全套的,从接口到抽象类,到实现抽象类,到使用实现后的类的方法,嘿嘿~
  1. public class Exercise25_01 {
  2.         public static void main(String[] args) {
  3.                 MyArrayList<String> list1 = new MyArrayList<String>();
  4.                 list1.add("Tom");
  5.                 list1.add("George");
  6.                 list1.add("Peter");
  7.                 list1.add("Jean");
  8.                 list1.add("Jane");

  9.                 MyArrayList<String> list2 = new MyArrayList<String>();
  10.                 list2.add("Tom");
  11.                 list2.add("George");
  12.                 list2.add("Michael");
  13.                 list2.add("Michelle");
  14.                 list2.add("Daniel");

  15.                 list1.addAll(list2);
  16.                 System.out.println(list1);

  17.                 list1.removeAll(list2);
  18.                 System.out.println(list1);

  19.                 list1.retainAll(list2);
  20.                 System.out.println(list1);
  21.         }

  22. }

  23. interface MyList<E> {
  24.         public void add(E e);

  25.         public void add(int index, E e);

  26.         public void clear();

  27.         public boolean contains(E e);

  28.         public E get(int index);

  29.         public int indexOf(E e);

  30.         public boolean isEmpty();

  31.         public int lastIndexOf(E e);

  32.         public boolean remove(E e);

  33.         public E remove(int index);

  34.         public Object set(int index, E e);

  35.         public int size();

  36.         public boolean addAll(MyList<E> otherList);

  37.         public boolean removeAll(MyList<E> otherList);

  38.         public boolean retainAll(MyList<E> otherList);

  39. }

  40. abstract class MyAbstrcatList<E> implements MyList<E> {
  41.         protected int size = 0;

  42.         protected MyAbstrcatList() {
  43.         }

  44.         protected MyAbstrcatList(E[] object) {
  45.                 for (int i = 0; i < object.length; i++)
  46.                         add(object[i]);
  47.         }

  48.         public void add(E e) {
  49.                 add(size, e);
  50.         }

  51.         public boolean addAll(MyList<E> otherList) {
  52.                 if (otherList != null) {
  53.                         addAll(otherList);
  54.                         return true;
  55.                 } else
  56.                         return false;
  57.         }

  58.         public boolean removeAll(MyList<E> otherList) {
  59.                 if (otherList != null) {
  60.                         removeAll(otherList);
  61.                         return true;
  62.                 } else
  63.                         return false;
  64.         }

  65.         public boolean retainAll(MyList<E> otherList) {
  66.                 if (otherList != null) {
  67.                         retainAll(otherList);
  68.                         return true;
  69.                 } else
  70.                         return false;
  71.         }

  72.         public boolean isEmpty() {
  73.                 return size == 0;
  74.         }

  75.         public int size() {
  76.                 return size;
  77.         }

  78.         public boolean remove(E e) {
  79.                 if (indexOf(e) >= 0) {
  80.                         remove(indexOf(e));
  81.                         return true;
  82.                 }
  83.                 return false;
  84.         }

  85. }

  86. class MyArrayList<E> extends MyAbstrcatList<E> {
  87.         public static final int INITIAL_CAPACITY = 16;
  88.         private E[] data = (E[]) new Object[INITIAL_CAPACITY];

  89.         public MyArrayList() {

  90.         }

  91.         public MyArrayList(E[] objects) {
  92.                 for (int i = 0; i < objects.length; i++)
  93.                         add(objects[i]);
  94.         }

  95.         public void add(int index, E e) {
  96.                 ensureCapacity();
  97.                 for (int i = size - 1; i >= index; i--)
  98.                         data[i] = data[i + 1];

  99.                 data[index] = e;

  100.                 size++;
  101.         }

  102.         private void ensureCapacity() {
  103.                 if (size >= data.length) {
  104.                         E[] newData = (E[]) new Object[2 * size + 1];
  105.                         System.arraycopy(data, 0, newData, 0, size);
  106.                         data = newData;
  107.                 }
  108.         }

  109.         public void clear() {
  110.                 data = (E[]) new Object[INITIAL_CAPACITY];
  111.                 size = 0;
  112.         }

  113.         public boolean contains(E e) {
  114.                 for (int i = 0; i < size; i++)
  115.                         if (e.equals(data[i]))
  116.                                 return true;
  117.                 return false;
  118.         }

  119.         public E get(int index) {
  120.                 return data[index];
  121.         }

  122.         public int indexOf(E e) {
  123.                 for (int i = 0; i < size; i++)
  124.                         if (e.equals(data[i]))
  125.                                 return i;
  126.                 return -1;
  127.         }

  128.         public int lastIndexOf(E e) {
  129.                 for (int i = size - 1; i >= 0; i--)
  130.                         if (e.equals(data[i]))
  131.                                 return i;
  132.                 return -1;
  133.         }

  134.         public E remove(int index) {
  135.                 E e = data[index];

  136.                 for (int i = index; i < size - 1; i++)
  137.                         data[i] = data[i + 1];

  138.                 data[size - 1] = null;

  139.                 size--;

  140.                 return e;
  141.         }

  142.         public E set(int index, E e) {
  143.                 E old = data[index];
  144.                 data[index] = e;
  145.                 return old;
  146.         }

  147.         public String toString() {
  148.                 StringBuilder result = new StringBuilder("[");

  149.                 for (int i = 0; i < size; i++) {
  150.                         result.append(data[i]);
  151.                         if (i < size - 1)
  152.                                 result.append(", ");
  153.                 }

  154.                 return result.toString() + "]";
  155.         }

  156.         public void trimToSize() {
  157.                 if (size != data.length) {
  158.                         E[] newData = (E[]) new Object[size];
  159.                         System.arraycopy(data, 0, newData, 0, size);
  160.                         data = newData;
  161.                 }
  162.         }

  163.         public boolean removeAll(MyList<E> otherList) {
  164.                 for (int i = 0; i < otherList.size(); i++) {
  165.                         remove(otherList.get(i));
  166.                 }

  167.                 for (int i = 0; i < otherList.size(); i++) {
  168.                         if (!data.equals(otherList.get(i)))
  169.                                 return false;
  170.                 }

  171.                 return true;
  172.         }

  173.         public boolean retainAll(MyList<E> otherList) {
  174.                 if (!removeAll(otherList))
  175.                         return false;
  176.                 if (!removeAll(otherList))
  177.                         return false;
  178.                 return true;
  179.         }

  180.         public boolean addAll(MyList<E> otherList) {
  181.                 for (int i = 0; i < otherList.size(); i++)
  182.                         add(otherList.get(i));
  183.                 for (int i = 0; i < otherList.size(); i++) {
  184.                         if (data.equals(otherList.get(i)))
  185.                                 return false;
  186.                 }

  187.                 return true;

  188.         }
  189. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
王渠 发表于 2012-7-26 19:19
o(∩_∩)o 哈哈,满足你哦,全套的,从接口到抽象类,到实现抽象类,到使用实现后的类的方法,嘿嘿~ ...

看起来像数据结构与算法课程里要写的顺序表。
回复 使用道具 举报
黑马刘涛 发表于 2012-7-26 19:25
看起来像数据结构与算法课程里要写的顺序表。

是自己做的书籍的里面的题目哦,而且的确全是自己写的。
是先学习了泛型,再学习集合后,做的题目。也应该和数据结构有一定关系吧
回复 使用道具 举报
{:soso_e135:}   完全看不懂。。。 先存起来  学到了 再拿出来瞧瞧。
回复 使用道具 举报
王渠 发表于 2012-7-26 19:31
是自己做的书籍的里面的题目哦,而且的确全是自己写的。
是先学习了泛型,再学习集合后,做的题目。也应 ...

写的不错,看起来很清爽。我再好好看看:lol
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马