zoukankan      html  css  js  c++  java
  • 类与对象

    一、           类和对象基础题

    9.创建一个三角形类,成员变量三边,方法求周长,创建类主类A来测试它。

    package com.hanqi.test;
     
    public class sanjiaoxing {
         private double a;
         private double b;
         private double c;
        public double getA() {
            return a;
        }
        public void setA(double a) {
            this.a = a;
        }
        public double getB() {
            return b;
        }
        public void setB(double b) {
            this.b = b;
        }
        public double getC() {
            return c;
        }
        public void setC(double c) {
            this.c = c;
        }
         
    public double getZhouchang()
        {
            return a+b+c;
        }
     
    }
    
    
    package com.hanqi.test;
    
    public class Test07 {
    
        public static void main(String[] args) {
            sanjiaoxing sjx=new sanjiaoxing();
            sjx.setA(10);
            sjx.setB(20);
            sjx.setC(30);
            System.out.println("三边分别是10,20,30的周长是:"+sjx.getZhouchang());
    
        }
    
    }

    10.按要求编写Java应用程序。

    (1)创建一个叫做People的类:

    属性:姓名、年龄、性别、身高

    行为:说话、计算加法、改名

    编写能为所有属性赋值的构造方法;

    (2)创建主类:

    创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80;

    让该对象调用成员方法:

    说出“你好!”

    计算23+45的值

    将名字改为“李四”

    复制代码
    package com.homework.zw;
    
    public class People
    {
       public String name;
       public int age;
       public String sex;
       public String height;
       
       People(String name, int age, String sex, String height)
       {
           this.name=name;
           this.age=age;
           this.sex=sex;
           this.height=height;
       }
       
       public String speak(String hua)
       {
           return hua;
       }
       public int add(int a,int b)
       {
           return a+b;
       }
       public String changename(String name)
       {
           this.name=name;
           return name;
       }
    }
    复制代码
    复制代码
    package com.homework.zw;
    
    public class Text_people 
    {
    
        public static void main(String[] args) 
        {
            People pe = new People("张三",18,"男","1.80米");
            System.out.println("姓名:"+pe.name+"  年龄:"+pe.age+"  性别:"+pe.sex+"  身高:"+pe.height);
            System.out.println("张三说:"+pe.speak("你好"));        
            System.out.println("23+45="+pe.add(23, 45));
            System.out.println("姓名改为:"+pe.changename("李四"));
            
        }
    
    }
    复制代码

    11.按要求编写Java应用程序。

    (1)创建一个叫做机动车的类:

    属性:车牌号(String),车速(int),载重量(double)

    功能:加速(车速自增)、减速(车速自减)、修改车牌号,查询车的载重量。

    编写两个构造方法:一个没有形参,在方法中将车牌号设置“XX1234”,速

    度设置为100,载重量设置为100;另一个能为对象的所有属性赋值;

    (2)创建主类:

    在主类中创建两个机动车对象。

    创建第一个时调用无参数的构造方法,调用成员方法使其车牌为“辽

    A9752”,并让其加速。

    创建第二个时调用有参数的构造方法,使其车牌为“辽B5086”,车速为150,

    载重为200,并让其减速。

    输出两辆车的所有信息

    package com.hanqi.test;
     
    public class jidongche {
        private String chepaihao;//车牌号
        private int speed;//速度
        private double weight;//载重量
        //无参构造方法
        jidongche()
        {
             
        }
        public String getChepaihao() {
            return chepaihao;
        }
        public void setChepaihao(String chepaihao) {
            this.chepaihao = chepaihao;
        }
        public int getSpeed() {
            return speed;
        }
        public void setSpeed(int speed) {
            this.speed = speed;
        }
        public double getWeight() {
            return weight;
        }
        public void setWeight(double weight) {
            this.weight = weight;
        }
        //有参构造方法
        jidongche(String cph,int sp,double wh)
        {
            chepaihao=cph;
            speed=sp;
            weight=wh;
             
        }
        //设置成员方法
        public int getAddspeed(int add)
        {
            return speed+=add;
        }
     
        public int getjianspeed(int jian)
        {
            return speed-=jian;
        }
        public void setchepaihap(String chage)
        {
            chepaihao=chage;
        }
        

      

    复制代码
    jidongche jdc=new jidongche();
            jdc.setChepaihao("xx1234");
            jdc.setSpeed(100);
            jdc.setWeight(100);
            System.out.println("无参构造方法车牌号为:"+jdc.getChepaihao()+"
    速度为:"+jdc.getSpeed()+"
    载重量为:"+jdc.getWeight());
            
            
            jdc.setChepaihao("辽A9752");
            jdc.getAddspeed(30);
            System.out.println("无参构造方法将车牌号改为:"+jdc.getChepaihao()+"
    速度增加到:"+jdc.getSpeed());
            
            jidongche jdc1=new jidongche("xx1234", 150, 100);
            jdc1.getjianspeed(30);
            jdc1.setchepaihap("辽B5086");
            System.out.println("有参构造方法将车牌号改为:"+jdc1.getChepaihao()+"
    速度减为:"+jdc1.getSpeed());
            System.out.println("该车辆的载重量为:"+jdc1.getWeight());
            
    复制代码

    12.创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方

    法初始化x和y。创建类主类A来测试它。

    package com.hanqi.test;
     
    public class Point {
        private int x;
        private int y;
        Point(int xx,int yy)
        {
            x=xx;
            y=yy;
        }
      public  int getX()
        {
         return x;
        }
        public int getY()
        {
            return y;
        }
         
    }

      

    Point pt=new Point(1,2);
            System.out.println("x值是"+pt.getX()+"y的值是"+pt.getY());
            

    13.首先,编写一个类ChongZai,该类中有3个重载的方法void print();其次,

    再编写一个主类来测试ChongZai类的功能。

    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package liu0919;
     
    public class ChongZai {
         
         public int max(int a, int b) {
     
                return a >= b ? a : b;
     
            }
             
         //参数个数相同,数据类型不相同,不冲突
            public double max(double a, double b) {
                return a >= b ? a : b;
            }
     
             
            //参数个数不同,数据类型相同,不冲突
            public double max(double a, double b, double c) {
                return max(max(a, b), c);
            }
         
    }

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package liu0919;
     
    public class Ceshi_chongzai {
     
        public static void main(String[] args) {
            ChongZai cz=new ChongZai ();
            System.out.println("58和12大的数是:"+cz.max(5812));
            System.out.println("58.12和12.24大的数是:"+cz.max(58.1212.24));
            System.out.println("18.1与25.5和30.0比较大的是:"+cz.max(18.125.530.0));
        }
     
    }

      

    package com.hanqi.test;
     
    public class Point {
        private int x;
        private int y;
        Point(int xx,int yy)
        {
            x=xx;
            y=yy;
        }
      public  int getX()
        {
         return x;
        }
        public int getY()
        {
            return y;
        }
         
    }

      

    Point pt=new Point(1,2);
            System.out.println("x值是"+pt.getX()+"y的值是"+pt.getY());
            

  • 相关阅读:
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    Windows邮件添加QQ邮箱
  • 原文地址:https://www.cnblogs.com/hanruyue/p/5887111.html
Copyright © 2011-2022 走看看