当前位置: 移动技术网 > IT编程>开发语言>Java > 学习java的第9天

学习java的第9天

2020年07月12日  | 移动技术网IT编程  | 我要评论

01. Math类的了解

  • 猜数字小游戏
import java.util.Scanner;

/**
 * Math类之猜大小
 * @author 海锋
 *
 */
public class Demo01_Math {
	public static void main(String[] args) {
		int num = (int)(Math.random()* 100) + 1;
		Scanner input = new Scanner(System.in);
		//循环次数初始化
		int i = 0;
		while(true) {
			
			System.out.println("输入一个数(1~100):");
			int num1 = input.nextInt();
			//猜大了
			if(num1 > num) {
				
				System.out.println("你猜大了");
			//猜小了
			}else if(num1 < num) {
				System.out.println("你猜小了");
				//猜中了
			}else {
				break;
			}
			//循环次数
			i++;
		}
		System.out.println("你猜中了!!你猜了" + i + "次");
	}

}

02. 代码块

  • 概念
    • 在java中,使用{}括起来的代码被称为代码块
  • 分类
    • 静态代码块
      • 将代码定义在类的成员位置,并使用static修饰,用来初始化静态变量,在构造代码块之前执行
    • 构造代码块
      • 将代码块定义在类的成员位置,用来存放构造器中的公共代码,在构造器之前执行
    • 局部代码块
      • 将代码定义在方法中,用来限定变量的生命周期
  • 静态代码块、构造代码块、构造器的执行顺序、执行次数
    • 执行顺序
      • 静态代码块->构造代码块->构造器
    • 执行次数
      • 静态代码块
        • 随着类的加载,且只加载一次,在对象创建之前执行
      • 构造代码块
        • 随着对象的创建而执行,对象创建多少次就执行多少次,在对象创建之前执行
      • 构造器
        • 随着对象的创建而执行,对象创建多少次就执行多少次,在对象创建时执行

03. 继承

  • 定义动物类,猫类,狗类不使用继承

    • Animal.java

      /**
       * 定义动物类
       * @author 海锋
       *
       */
      public class Animal {
      	private String name;
      	private String color;
      	private int age;
      	
      	public Animal() {
      		
      	}
      	public Animal(String name , String color, int age) {
      		this.name = name;
      		this.color = color;
      		this.age = age;
      		
      	}
         public String getName() {
      	   return name;
         }
         public void setName(String name) {
      	   this.name = name;
         }
         public String getColor() {
      	   return color;
         }
         public void setColor(String color) {
      	   this.color = color;
         }
         public int getAge() {
      	   return age;
         }
         public void setAge(int age) {
      	   this.age = age;
         }
         
      }
      
      
    • Cat.java

      /**
       * 定义猫类
       * @author 海锋
       *
       */
      public class Cat {
      	private String name;
      	private String color;
      	private int age;
      	
      	public Cat() {
      		
      	}
      	public Cat(String name , String color, int age) {
      		this.name = name;
      		this.color = color;
      		this.age = age;
      		
      	}
         public String getName() {
      	   return name;
         }
         public void setName(String name) {
      	   this.name = name;
         }
         public String getColor() {
      	   return color;
         }
         public void setColor(String color) {
      	   this.color = color;
         }
         public int getAge() {
      	   return age;
         }
         public void setAge(int age) {
      	   this.age = age;
         }
         public void eat() {
      	   System.out.println("吃鱼");
         }
      }
      
      
    • Dog.java

      /**
       * 定义狗类
       * @author 海锋
       *
       */
      public class Dog {
      	private String name;
      	private String color;
      	private int age;
      	public Dog() {
      		
      	}
      	public Dog(String name , String color, int age) {
      		this.name = name;
      		this.color = color;
      		this.age = age;
      		
      	}
         public String getName() {
      	   return name;
         }
         public void setName(String name) {
      	   this.name = name;
         }
         public String getColor() {
      	   return color;
         }
         public void setColor(String color) {
      	   this.color = color;
         }
         public int getAge() {
      	   return age;
         }
         public void setAge(int age) {
      	   this.age = age;
         }
         public void lookHome() {
      	   System.out.println("看家");
         }
      }
      
      
  • 定义动物类,猫类,狗类不使用继承

    • Animal.java

      /**
       * 定义动物类
       * @author 海锋
       *
       */
      public class Animal {
      	private String name;
      	private String color;
      	private int age;
      	
      	public Animal() {
      		
      	}
      	public Animal(String name , String color, int age) {
      		this.name = name;
      		this.color = color;
      		this.age = age;
      		
      	}
         public String getName() {
      	   return name;
         }
         public void setName(String name) {
      	   this.name = name;
         }
         public String getColor() {
      	   return color;
         }
         public void setColor(String color) {
      	   this.color = color;
         }
         public int getAge() {
      	   return age;
         }
         public void setAge(int age) {
      	   this.age = age;
         }
         
      }
      
      
    • Cat.java

      /**
       * 定义猫类
       * @author 海锋
       *
       */
      public class Cat1 extends Animal{
      	 public void eat() {
      	   System.out.println("吃鱼");
         }
      	
      
      }
      
    • Dog.java

      /**
       * 定义狗类
       * @author 海锋
       *
       */
      public class Dog1 extends Animal{
      	 public void lookHome() {
      	   System.out.println("看家");
         }
      	
      
      }
      
  • 继承的好处和弊端

    • 继承的好处

      • 提高了代码的复用性
      • 使类和类之间存在继承关系,是多态的前提
    • 弊端

      • 提高了耦合性
      • java代码讲究低耦合高内聚
  • java中继承的特点及注意事项

    • 特点

      • 如果要使用继承体系中所有功能,那么就需要创建最底层的对象
      • 如果要使用继承体系中公共功能,那么就需要创建最顶层的对象
    • 注意事项

      • 类与类只能是单继承,不能是多继承
      • 类与类可以是多层继承
      • 子类不能继承父类的私有成员
      • 子类不能继承父类的构造器
    • 使用场景

      • 需要两个类满足“is a”关系,就可以设置为继承关系

      • 里氏替换原则

  • 继承中成员变量的关系

    • 如果子类中没有,那就从父类找
    • 如果父类中没有,那就会报错

本文地址:https://blog.csdn.net/haifengtongxue/article/details/107263455

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网