zoukankan      html  css  js  c++  java
  • Java日志第22天 2020.7.27

    类作为成员变量类型

    public class Hero {
    
        private String name;
        private int age;
        private Weapon weapon;
    
        public Hero(){
        }
    
        public Hero(String name, int age, Weapon weapon) {
            this.name = name;
            this.age = age;
            this.weapon = weapon;
        }
    
        public void attack(){
            System.out.println(age+ "岁的" + name + "使用" + weapon.getWeaponName()+"进行攻击");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Weapon getWeapon() {
            return weapon;
        }
    
        public void setWeapon(Weapon weapon) {
            this.weapon = weapon;
        }
    }
    public class Weapon {
    
        private String weaponName;
    
    
        public Weapon(){
        }
    
        public Weapon(String weaponName){
            this.weaponName = weaponName;
        }
    
        public String getWeaponName() {
            return weaponName;
        }
    
        public void setWeaponName(String weaponName) {
            this.weaponName = weaponName;
        }
    }
    public class DemoMain {
    
        public static void main(String[] args) {
            Hero hero = new Hero();
    
            hero.setName("亚瑟");
            hero.setAge(15);
    
            hero.setWeapon(new Weapon("大宝剑"));
    
            hero.attack();
        }
    }

    结果如下:

    接口作为成员变量类型

    public interface Skill {
        public void realseSkill();
    }
    public class Hero {
        private String name;
        private Skill skill;
    
        public Hero(){
        }
    
        public Hero(String name, Skill skill) {
            this.name = name;
            this.skill = skill;
        }
    
        public void attack(){
            System.out.print(name + "使用");
            skill.realseSkill();
    
            System.out.println("释放技能完成!");
        }
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Skill getSkill() {
            return skill;
        }
    
        public void setSkill(Skill skill) {
            this.skill = skill;
        }
    }
    public class DemoMain {
    
        public static void main(String[] args) {
            Hero hero = new Hero();
    
            hero.setName("亚瑟");
    
            Skill skill = new Skill() {
                @Override
                public void realseSkill() {
                    System.out.println("震雷削");
                }
            };
            hero.setSkill(skill);
            hero.attack();
        }
    }

    接口作为方法的参数或返回值

    import java.util.ArrayList;
    import java.util.List;
    
    public class DemoInterface {
        public static void main(String[] args) {
            //左边是接口名称,右边是实现类名称,这是多态写法
            List<String> list = new ArrayList<>();
    
            list = addNames(list);
    
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
        }
    
        public static List<String> addNames(List<String> list){
            list.add("迪丽热巴");
            list.add("古力娜扎");
            list.add("马尔扎哈");
            list.add("沙扬娜拉");
            return list;
        }
    }

    结果如下:

     

    今天的内容很简单,但应用到以前的知识还是很多的遗忘,有些代码的过程有些模糊用法。

    不能这样啊啊,还是要多多复习,熟练掌握用法,不能应用到稍微复杂的案例是就搞不清用法。

    明天开始Object这个最高父类的学习了!

  • 相关阅读:
    Python3 CGI编程实现教程
    SSL密钥协商过程分析
    浏览器同源策略理解
    Python3+selenium 报错处理:“selenium.common.exceptions.NoAlertPresentException: Message: No alert is active”
    Python3 try-except、raise和assert解析
    计算机视觉常见技术(待理解)
    中国大学MOOC-陈越、何钦铭-数据结构-2017春
    Coursera机器学习+deeplearning.ai+斯坦福CS231n
    总结一些机器视觉库
    git rebase 多分支操作
  • 原文地址:https://www.cnblogs.com/Gazikel/p/13384199.html
Copyright © 2011-2022 走看看