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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

1.抽象类的应用---定义模板---WEB开发abstract class Person{        private int age;        private String name;        public Person(String name, int age){          this.name=name;                  this.age=age;        }        public String getName(){          return this.name;        }        public int getAge(){          return this.age;        }        public abstract void say();
}
class Student extends Person{        int score;        public Student(String name,int age,int score){          super(name,age);          this.score=score;        }    public int getScore(){          return this.score;        }
        public void say(){         System.out.println("名字:  "+super.getName()+"年龄:  "+super.getAge()+"分数:"+this.getScore());        }}class Worker extends Person{        int salary;        public Worker(String name,int age,int salary){          super(name,age);          this.salary=salary;        }    public int getSalary(){          return this.salary;        }
        public void say(){         System.out.println("名字:  "+super.getName()+"年龄:  "+super.getAge()+"薪水:"+this.getSalary());        }}
public class NewDemo{        public static void main(String [] args){          Person per1=new Student("海峰",20,40);          Person per2=new Worker("邵阳",34,40000);
     per1.say();         per2.say();
        }}运行的结果为:

2.接口类的应用---定义标准interface USB{  void start();  void stop();}
class Computer{        public static void plugin(USB b){          b.start();          System.out.println("-------USB 设备工作");          b.stop();        }}class Flash implements  USB{        public void start(){          System.out.println("U盘开始工作");        }        public void stop(){          System.out.println("U盘停止工作");        }}
class Print implements USB{        public void start(){          System.out.println("打印机开始工作");        }        public void stop(){          System.out.println("打印机停止工作");        }}
public class  NewDemo{  public static void main(String [] args){     Computer.plugin(new Flash());// 相当于 我只是要USB这个标准  只要你符合这个标准我都可以用         Computer.plugin(new Print());  }}
运行的结果为:

1 个回复

倒序浏览
1.抽象类的应用---定义模板---WEB开发

  1. <p>abstract class Person{</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>private int age;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>private String name;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public Person(String name, int age){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  this.name=name;<span class="Apple-tab-span" style="white-space:pre">        </span></p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  this.age=age;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public String getName(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  return this.name;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public int getAge(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  return this.age;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public abstract void say();</p><p>
  2. </p><p>}</p><p>
  3. </p><p>class Student extends Person{</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>int score;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public Student(String name,int age,int score){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  super(name,age);</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  this.score=score;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>    public int getScore(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  return this.score;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>
  4. </p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public void say(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span> System.out.println("名字:  "+super.getName()+"年龄:  "+super.getAge()+"分数:"+this.getScore());</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>}</p><p>class Worker extends Person{</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>int salary;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public Worker(String name,int age,int salary){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  super(name,age);</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  this.salary=salary;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>    public int getSalary(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  return this.salary;</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>
  5. </p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public void say(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span> System.out.println("名字:  "+super.getName()+"年龄:  "+super.getAge()+"薪水:"+this.getSalary());</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>}</p><p>
  6. </p><p>public class NewDemo{</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public static void main(String [] args){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  Person per1=new Student("海峰",20,40);</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  Person per2=new Worker("邵阳",34,40000);</p><p>
  7. </p><p>     per1.say();</p><p><span class="Apple-tab-span" style="white-space:pre">        </span> per2.say();</p><p>
  8. </p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>}</p><p>运行的结果为:</p>
复制代码



2.接口类的应用---定义标准

  1. <p>interface USB{</p><p>  void start();</p><p>  void stop();</p><p>}</p><p>
  2. </p><p>class Computer{</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public static void plugin(USB b){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  b.start();</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  System.out.println("-------USB 设备工作");</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  b.stop();</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>}</p><p>class Flash implements  USB{</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public void start(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  System.out.println("U盘开始工作");</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public void stop(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  System.out.println("U盘停止工作");</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>}</p><p>
  3. </p><p>class Print implements USB{</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public void start(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  System.out.println("打印机开始工作");</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>public void stop(){</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>  System.out.println("打印机停止工作");</p><p><span class="Apple-tab-span" style="white-space:pre">        </span>}</p><p>}</p><p>
  4. </p><p>public class  NewDemo{</p><p>  public static void main(String [] args){</p><p>     Computer.plugin(new Flash());// 相当于 我只是要USB这个标准  只要你符合这个标准我都可以用</p><p><span class="Apple-tab-span" style="white-space:pre">        </span> Computer.plugin(new Print());</p><p>  }</p><p>}</p><p></p>
复制代码



运行的结果为:



回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马