Java基础编程分享心得

一、抽象练习

定义抽象类Person,类中包含name属性和抽象方法speak(),定义子类Teacher类和子类Worker类,分别实现抽象方法speak(),分别显示教师说的话和工人说的话。在测试类Main中定义Teacher类和Worker类的上转型对象,调用speak()方法显示信息。

main函数的代码如下,不要修改下面代码,否则会扣分!

public static void main(String[] args) {

Person p1 = new Teacher("Mr wang");

p1.speak();

p1 = new Worker("Worker zhang");

p1.speak();

}

输出结果:

Mr wang said class is over!

Worker zhang said to have a rest!

要求:

(1)必须有抽象类和抽象方法、继承、重写和上转型

(2)“Mr wang”和“Worker zhang”分别是name属性的值

(3)name属性必须在Person类定义,Teacher类和Worker不需要定义

(4)类中的属性必须是私有属性

(5)理解什么是多态


【输入形式】
【输出形式】

Mr wang said class is over!

Worker zhang said to have a rest!





public class Test {
    public static void main(String[] args) {

        Person p1 = new Teacher("Mr wang");

        p1.speak();

        p1 = new Worker("Worker zhang");

        p1.speak();

    }

}
abstract class Person{
    private String name;
    public Person(String name){
        this.name=name;
    }
    public void speak(){
        System.out.println("said class is over!");
    }
    public String getName(){
        return name;
    }
}
class Teacher extends Person{



    public Teacher(String mrWang) {
        super(mrWang);
    }


    public void speak(){
        System.out.println(getName()+" said class is over!");
    }
}
class Worker extends Person{
    public Worker(String name){
        super(name);
    }
    public void speak(){
        System.out.println(getName()+" said to have a rest!");
    }
}

二、接口编程一

要求编程实现Student类,使给定的Test类能正常运行,并实现按字母顺序输出姓名。

import java.util.Arrays;
public class Test
{
    public static void main(String[] args)
    {
        Student[] s = new Student[4];
        s[0] = new Student("May");
        s[1] = new Student("Jack");
        s[2] = new Student("Armstrong");
        s[3] = new Student("Linda");

        Arrays.sort(s);

        System.out.println("according to alphabetical order:");
        for (Student stu:s)
        {
            System.out.println(stu.getName());
        }
    }
}

Arrays.sort()方法是Java已经实现好的工具类Arrys中用于对数组元素进行排序的方法。它要求数组中的元素已经实现了Comparable接口。

Comparable接口是Java已经实现好的一个接口,该接口主要代码如下:
public interface Comparable<T> {
     public int compareTo(T o);
 }
在Java程序中可以直接使用它。
接口的CompareTo()方法定义为:
    返回负数:如果调用对象比参数o"小"。这里"小"的具体含义由程序员自己通过代码来定义;
    返回正数:如果调用对象比参数o"大"。这里"大"的具体含义由程序员自己通过代码来定义;
    返回零:如果调用对象与参数o"相等"。这里"相等"的具体含义由程序员自己通过代码来定义。

提示:可以查看JavaAPI文档,了解更多相关信息。

【输入形式】
【输出形式】
【样例输入】
【样例输出】
according to alphabetical order:
Armstrong
Jack
Linda
May



import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        Student[] s = new Student[4];
        s[0] = new Student("May");
        s[1] = new Student("Jack");
        s[2] = new Student("Armstrong");
        s[3] = new Student("Linda");

        Arrays.sort(s);

        System.out.println("according to alphabetical order:");
        for (Student stu : s) {
            System.out.println(stu.getName());
        }
    }
}
class Student implements Comparable<Student> {
    private String name;

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int compareTo(Student o) {
        return this.name.compareTo(o.getName());
    }
}


三、接口编程2

要求编程实现Student类,使给定的Test类能正常运行,并实现按长度顺序输出姓名。

import java.util.Arrays;
public class Test
{
    public static void main(String[] args)
    {
        Student[] s = new Student[4];
        s[0] = new Student("May");
        s[1] = new Student("Jack");
        s[2] = new Student("Armstrong");
        s[3] = new Student("Linda");

        Arrays.sort(s);

        System.out.println("according to length order:");
        for (Student stu:s)
        {
            System.out.println(stu.getName());
        }
    }
}

Arrays.sort()方法是Java已经实现好的工具类Arrys中用于对数组元素进行排序的方法。它要求数组中的元素已经实现了Comparable接口。

Comparable接口是Java已经实现好的一个接口,该接口主要代码如下:
public interface Comparable<T> {
     public int compareTo(T o);
 }
在Java程序中可以直接使用它。
接口的CompareTo()方法定义为:
    返回负数:如果调用对象比参数o"小"。这里"小"的具体含义由程序员自己通过代码来定义;
    返回正数:如果调用对象比参数o"大"。这里"大"的具体含义由程序员自己通过代码来定义;
    返回零:如果调用对象与参数o"相等"。这里"相等"的具体含义由程序员自己通过代码来定义。

提示:可以查看JavaAPI文档,了解更多相关信息。

【输入形式】
【输出形式】
【样例输入】
【样例输出】

according to length order:
May
Jack
Linda
Armstrong



import java.util.Arrays;


class Sdent implements Comparable<Sdent> {
    private String name;

    public Sdent(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int compareTo(Sdent o) {
        return this.name.length() - o.getName().length();
    }
}

public class Test {
    public static void main(String[] args) {
        Sdent[] s = new Sdent[4];
        s[0] = new Sdent("May");
        s[1] = new Sdent("Jack");
        s[2] = new Sdent("Armstrong");
        s[3] = new Sdent("Linda");

        Arrays.sort(s);

        System.out.println("according to length order:");
        for (Sdent stu : s) {
            System.out.println(stu.getName());
        }
    }
}

四、程序片段题

public  class  Test{
        public  static  void  main(String[]  args)  
        {
                TableInfo  t1=new  RoundTable  (3,  100,  30.0);
                TableInfo  t2=new  RectangleTable(4,  100,  40.0,  60.0);
                System.out.println("Round  Table  Area"  +  t1.tableArea());
        System.out.println("Rect  Table  Area"  +  t2.tableArea());
        }
}

       abstract class TableInfo{
        int  legs;
        int  hight;

        public    TableInfo(  int  legs,int  hight){
                this.legs  =  legs;
                this.hight  =  hight;
        }

        public  abstract  double  tableArea();
}

class  RectangleTable  extends  TableInfo{
        private  double  len;
        private  double  width;

        public  RectangleTable(  int  legs,  int  hight,  double  len,  double  width){
                super(legs,hight);
                this.len  =  len;
                this.width  =  width;
        }

        public  double  tableArea(){
                return  len  *  width;
        }
}

class  RoundTable  extends  TableInfo{
        private  double  r;

        public  RoundTable(  int  legs,  int  hight,  double  r){
                super(legs,  hight);
                this.r  =  r;
        }

        public  double  tableArea(){
                  return  3.14  *  r  *  r;
        }
}

五、接口题

1、实现接口MoveAble显示学生移动信息



public class Test {
    public static void main(String[] args){
        Te s=new Te();
        s.move();
    }
}
interface Moveable{
    void move();
}
class Te implements Moveable{
    public void move(){
        System.out.println("move with talk");
    }
}

2、涉及教师类实现接口MoveAble

public class Test {
    public static void main(String[] args){
        Te s=new Te();
        s.move();
    }
}
interface Moveable{
   void move();
}
class Te implements  Moveable {
    public void move(){
        System.out.println("move with talk");
    }
}

3、继承抽象类Phone,实现手机类,显示手机信息。

public class Test {
    public static void main(String[] args){
        Startphone stp=new Startphone();
        stp.display();
    }
}
abstract class Phone{
    abstract void display();
}
class Startphone extends Phone{
    public void display(){
        System.out.println("Brand isHUAWEI");
        System.out.println("OwnerId is130111111111111111");
    }
}

4、实现抽象类Shape,计算圆面积

import java.util.Scanner;

public class Test {
    public static void main(String[] aegs){
        Scanner sc=new Scanner(System.in);
        double radius= sc.nextDouble();
        sc.close();
        circl circle=new circl(radius);
        double c= circle.display();
        System.out.println("area="+c);
    }
}
abstract class Shape{
    abstract double display();
}
class circl extends Shape{
    private double radius;
    public circl(double radius){
        this.radius=radius;
    }
    public double display(){
        return 3.14*radius*radius;
    }
}

5、设计学生类Student,实现接口MoveAble中方法,显示学生移动信息

public class Test {
    public static void main(String[] args){
        StudentM sm=new StudentM();
        sm.move();
    }
}
interface Moveabl{
    void move();
}
class StudentM implements Moveabl{
    public void move(){
        System.out.println("move with owner");
    }
}

希望可以帮到到你,如果帮到您,能给一个赞吗和关注吗?谢谢

  • 20
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值