zoukankan      html  css  js  c++  java
  • 简单的方法使用案例一

    (一)、创建一个武器类Weapen
    定义类中属性:攻击力Power,速度speed,并实现构造方法
    创建一个Tank子类从Weapen类继承
    具有私有的方向字段dir(上下左右),并用属性封装
    定义攻击方法Attack(),打印“我是坦克,向**方向运动,速度***,攻击力***”

    创建一个子弹类Bullen从Weapen类继承
    具有私有的type字段(表示子弹类型,如:机枪子弹,步枪子弹),用属性封装
    定义攻击方法Attack(),打印”我是子弹***,速度***,攻击力***”
    为Tank类和Bullen类定义构造函数初始化属性(要求使用super调用父类的构造函数)
    创建一只Tank对象,调用其方法;创建一只Bullen,调用其方法

    package ja1;
    
    public class Weapen {
    	int power;//攻击力
    	double speed;//速度
    	public Weapen(int power,double speed){
    		this.power=power;
    		this.speed=speed;
    	}
    	
    
    }
    

      

    package ja1;
    
    public class Tank extends Weapen{
    	private String dir;//方向
    	public Tank(String dir){
    		super(45,23.3);
    		this.dir=dir;
    	}
    	public void Attack(){
    		
    		System.out.println("我是坦克,向"+dir+"方向运动,速度"+speed+",攻击力"+power);
    	}
    	
    
    }
    

      

    package ja1;
    
    import java.util.Scanner;
    
    public class Bullen extends Weapen{
    	private String type;
    	public Bullen(String type){
    		super(85,52.1);
    		this.type=type;
    	}
    	public void Attack(){
    		System.out.println("我是"+type+",速度"+speed+",攻击力"+power);
    	}
    
    }
    

      

    package ja1;
    import java.util.Scanner;
    public class Test {
    	public static void main(String[] args) {
    		Scanner input=new Scanner(System.in);
    		System.out.println("请输入坦克运动方向:");
    		String dir=input.next();
    		Tank tan=new Tank(dir);
    		tan.Attack();
    		System.out.println("请输入子弹类型:");
    		String type=input.next();
    		Bullen bul=new Bullen(type);
    		bul.Attack();
    	}
    
    }
    

      

  • 相关阅读:
    vim常用命令
    re正则表达式
    time和datetime
    airtestpython连接airtest及生产报告
    Java中的监视器(monitor)是什么?
    CentOS6.9安装MySQL5.7
    CentOS6不能使用yum问题修复:Cannot find a valid baseurl for repo: base
    谁说BI是新兴的有前途的技术,说穿了,就是原来的报表系统
    Trained myself of Informatica 6.0 (part 2 Installation).
    开始看lcc编译器的实现。---跟.net暂时扯不上关系。
  • 原文地址:https://www.cnblogs.com/www-x/p/8087632.html
Copyright © 2011-2022 走看看