明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
123
返回列表 发新帖
楼主: 不死猫

Java 自学日记

  [复制链接]
 楼主| 发表于 2012-7-29 16:08 | 显示全部楼层
上面这贴是我家孩子的回复。
private  不可复写,相当于局部的方法。
super访问父类方法

看过继承应用,默写一套

public class Test {
        public static void main(String[] args){
                //ReverseArray ar = new ReverseArray(5);
                SortArray ar = new SortArray(5);
                System.out.print(ar.add(2) + "\t");
                System.out.print(ar.add(1) + "\t");
                System.out.print(ar.add(5) + "\t");
                System.out.print(ar.add(3) + "\t");
                System.out.print(ar.add(4) + "\t");
                System.out.print(ar.add(6) + "\t");
                print(ar.getArray());
        }
        public static void print (int a[]){
                for (int i = 0;i < a.length;i++){
                        System.out.print(a[i]);
                }
        }
}
class Array{
        private int temp[];
        private int num = 0;
        public Array(int s){
                if (s < 1){
                        this.temp = new int[1];
                }else{
                        this.temp = new int[s];
                }
        }
        public boolean add(int s){
                if (temp.length <= num){
                        return false;
                }else{
                        temp[num]=s;
                        num++;
                        return true;
                }
        }
        public int[] getArray(){
                return temp;
        }
}
class ReverseArray extends Array{
        public ReverseArray(int len){
                super(len);
        }
        public int[] getArray(){
                int re[] = new int[super.getArray().length];
                for (int i = 0;i<re.length;i++){
                        re[i]=super.getArray()[super.getArray().length - i - 1];       
                }
                return re;
        }
}
class SortArray extends Array{
        public SortArray(int len){
                super(len);
        }
        public int[] getArray(){
                java.util.Arrays.sort(super.getArray());
                return super.getArray();
        }
}
发表于 2012-7-30 00:32 | 显示全部楼层
抽象类 看过默写


public class Test {
        public static void main(String[] args){
                B b = new B("123");
                b.print();
        }
}
abstract class A{
        public static final String DogName = "god";
        private String name = "123";
        public String getName(){
                return name;
        }
        public void setName(String name){
                this.name = name;
        }
        abstract void print();
}
class B extends A{
        public B(String name){
                super.setName(name);
        }
        void print() {
                System.out.print(DogName);
                System.out.print(super.getName());
        }
}
 楼主| 发表于 2012-7-30 09:48 | 显示全部楼层
再虚一个
public class Test {
        public static void main(String[] args) {
                Student st = new Student("a" , 1, "2");
                st.print();
        }
}
abstract class Person{
        private String name;
        private int age;
        public Person(String name,int age){
                this.name = name;
                this.age = age;
        }
        public String getName(){
                return name;
        }
        public int getAge(){
                return age;
        }
        abstract public void print();
}
class Student extends Person{
        private String school;
        public Student(String name,int age,String school){
                super(name,age);
                this.school = school;
        }
        public void print(){
                System.out.print(getName() + getAge() + school);
        }
}
 楼主| 发表于 2012-7-30 17:42 | 显示全部楼层
接口基础
public class Test{
        public static void main(String args[]){
                S s = new S();
                s.printA();
                s.printB();
                s.printC();
                s.printD();
                s.printE();
        }
}
interface A {
        public void printA();
}
interface B {
        public void printB();
}
interface C extends A,B{
        public void printC();
}
interface D {
        public void printD();
        public static final String OK = "ok";
}
abstract class E implements D{
        abstract void printE();
}
class S extends E implements C{
        public void printA(){
                System.out.print("A");
        }
        public void printB(){
                System.out.print("B");
        }
        public void printC(){
                System.out.print("C");
        }
        public void printD(){
                System.out.print("D");
        }
        public void printE(){
                System.out.print(OK);
        }
}
 楼主| 发表于 2012-7-31 18:02 | 显示全部楼层

开会很痛苦,一开一上午。
看一遍理解后再默写了一通
从6.7写到6.93




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2012-8-5 01:42 | 显示全部楼层
20120805 奥运会的20公里竞走搞定金牌
宠物商店案例默写
  1. interface Pet{
  2.         public String getName();
  3.         public String getColor();
  4.         public int getAge();
  5. }
  6. class Cat implements Pet{
  7.         String name;
  8.         String color;
  9.         int age;
  10.         public Cat(String name,String color,int age){
  11.                 this.name = name;
  12.                 this.color = color;
  13.                 this.age = age;
  14.         }
  15.         public String getName(){
  16.                 return this.name;
  17.         }
  18.         public String getColor(){
  19.                 return this.color;
  20.         }
  21.         public int getAge(){
  22.                 return this.age;
  23.         }
  24. }
  25. class Dog implements Pet{
  26.         String name;
  27.         String color;
  28.         int age;
  29.         public Dog(String name,String color,int age){
  30.                 this.name =name;
  31.                 this.color = color;
  32.                 this.age =age;
  33.         }
  34.         public String getName(){
  35.                 return this.name;
  36.         }
  37.         public String getColor(){
  38.                 return this.color;
  39.         }
  40.         public int getAge(){
  41.                 return this.age;
  42.         }
  43. }
  44. class PetShop{
  45.         Pet[] ps= null;
  46.         int foot = 0;
  47.         public PetShop(int num){
  48.                 if(num < 1){
  49.                         this.ps = new Pet[1];
  50.                 }
  51.                 else
  52.                 {
  53.                         this.ps = new Pet[num];
  54.                 }
  55.         }
  56.         public boolean Add(Pet ps){
  57.                 if (this.ps.length > foot){
  58.                         this.ps[foot] = ps;
  59.                         foot++;
  60.                         return true;
  61.                 }else{
  62.                         return false;
  63.                 }
  64.         }
  65.         public Pet[] Search(String content){
  66.                 Pet[] temp = null;
  67.                 int num =0;
  68.                 for (int i = 0;i<this.ps.length;i++){
  69.                         if(this.ps[i] != null){
  70.                                 if (this.ps[i].getName().indexOf(content) != -1 ||
  71.                                                 this.ps[i].getColor().indexOf(content) != -1)
  72.                                 {
  73.                                         num++;
  74.                                 }
  75.                         }
  76.                 }
  77.                 temp = new Pet[num];
  78.                 num = 0;
  79.                 for (int i = 0;i<this.ps.length;i++){
  80.                         if(this.ps[i] != null){
  81.                                 if (this.ps[i].getName().indexOf(content) != -1 ||
  82.                                                 this.ps[i].getColor().indexOf(content) != -1)
  83.                                 {
  84.                                         temp[num] = ps[i];
  85.                                         num++;
  86.                                 }
  87.                         }
  88.                 }
  89.                 return temp;
  90.         }
  91. }
  92. public class Test{
  93.         public static void main(String[] args){
  94.                 PetShop ps = new PetShop(5);
  95.                 ps.Add(new Cat("白猫","2",2));
  96.                 ps.Add(new Cat("黑猫","2",2));
  97.                 ps.Add(new Dog("白狗","2",2));
  98.                 ps.Add(new Cat("1","白色",2));
  99.                 ps.Add(new Cat("1","2",2));
  100.                 ps.Add(new Cat("1","2",2));
  101.                 Print(ps.Search("白"));
  102.         }
  103.         public static void Print(Pet ps[]){   
  104.                 for (int i = 0;i<ps.length;i++){
  105.                         System.out.print(ps[i].getName() + "\n");
  106.                 }
  107.         }
  108. }
复制代码
发表于 2014-7-4 21:57 | 显示全部楼层
谢谢分享!        
发表于 2015-6-16 17:05 | 显示全部楼层
不错,要是能有CAD WEB ,有平板能开发的CAD就好了
发表于 2017-10-30 11:10 | 显示全部楼层
回帖是一种美德!感谢楼主的无私分享 谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-20 02:30 , Processed in 1.120038 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表