Day08
昨日回顾
案例演示
1.学生类
/*
show方法和getXxx方法的区别:
*show只是为了显示属性值;
*getXxx获取属性值,可以打印,也可以赋值给其他的变量,做其他操作
*/
class Demo1_Student
{
public static void main(String[] args)
{
Student s1 = new Student(); //使用空参构造创建对象;
s1.setName("李四");
s1.setAge(25);
System.out.println("此人姓名是" + s1.getName() + ",此人年龄是" + s1.getAge());
System.out.println("------------");
Student s2 = new Student("张三",23);
s2.show();
}
}
class Student
{
private String name;
private int age;
public Student(){} //空参构造
public Student(String name ,int age){
this.name = name ;
this.age = age;
} // 有参构造
public void setName(String name){
this.name = name; //设置姓名
}
public String getName(){
return name; //获取姓名
}
public void setAge(int age){
this.age = age; //设置年龄
}
public int getAge(){
return age; //获取年龄
}
public void show(){
System.out.println("此人姓名是" + name + ",此人年龄是" + age);
} //输出姓名和年龄
}
2.手机类
class Demo2_Phone
{
public static void main(String[] args)
{
Phone p1 = new Phone();
p1.setBrand("锤子");
p1.setPrice(1499);
System.out.println("手机品牌是" + p1.getBrand() + ",价格是" + p1.getPrice());
System.out.println("-----------------");
Phone p2 = new Phone("红米note7",1499);
p2.show();
}
}
class Phone{
private String brand;
private int price;
public Phone(){};
public Phone(String brand,int price){
this.brand = brand;
this.price = price;
}
public void setBrand(String brand)
{
this.brand = brand;
} //设置品牌
public String getBrand(){
return brand;
} //获取品牌
public void setPrice(int price){
this.price = price ;
} //设置价格
public int getPrice(){
return this.price; //获取价格;
}
public void show(){
System.out.println("手机品牌是" + brand + ",价格是" + price);
} //显示成员属性
}
面向对象
创建一个对象的步骤
/* Student s = new Student();
*1.Student.class加载进内存方法区;
*2.声明一个Student的类型引用;
*3.在堆内存创建对象;
*4.给对象中的属性默认值初始化;
*5.属性进行显示初始化;
*6.构造方法进栈,对对象中的属性赋值,构造方法弹栈;
*7.将对象的地址值给到s
*/
class Demo3_Student {
public static void main(String[] args) {
Student s = new Student();
s.show();
}
}
class Student {
private String name = "张三";
private int age = 23;
public Student(){
name = "李四";
age = 24;
}
public void show(){
System.out.println(name + "..." + age);
}
}
作业案例
/*
* A:需求:顶一个长方形类,定义,求州长和面积的方法;
*然后定义一个测试类进行测试
分析:成员变量:宽度width 高height
空参构造,有参构造;
成员方法:
setXxx和getXxx
求周长:getLength;
求面积:getArea;
*/
import java.util.Scanner;
class Test1_Rectangle { //矩形
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Rectangle r = new Rectangle();
System.out.println("请输入矩形的长");
int width = sc.nextInt();
r.setWidth(width);
System.out.println("请输入矩形的宽");
int height = sc.nextInt();
r.setHeight(height);
System.out.println("矩形的周长是" + r.getLength());
System.out.println("矩形的面积是" + r.getArea());
}
}
class Rectangle {
private int height;
private int width;
public Rectangle(){}; //空参构造
public Rectangle(int width,int height){
this.width = width;
this.height = height; //有参构造
}
//setXxx和getXxx
public void setWidth(int width){
this.width = width;
}
public int getWidth(){
return width;
}
public void setHeight(int height){
this.height = height;
}
public int getHeight(){
return height;
}
//求周长
public int getLength(){
int length = (this.width + this.height) * 2 ;
return length;
}
public int getArea(){
int area = this.width * this.height ;
return area;
}
}
/*A:案例演示
*需求:定义一个员工类Employee
*自己分析出几个成员,然后给出成员变量;
*姓名 name 工号 id 工资 salary
*构造方法:空参构造和有参构造;
*getXxx()setXxx()方法;
*以及一个显示所有成员信息的方法;
*/
class Test2_Employee {
public static void main(String[] args) {
Employee e1 = new Employee();
e1.setName("王麻子");
e1.setId("A16235");
e1.setSalary(20000);
System.out.println("员工姓名是" + e1.getName() + ",工号是" + e1.getId() + ",薪资是" + e1.getSalary());
System.out.println("----------------");
Employee e2 = new Employee("黄俊","0103597",15000);
e2.show();
}
}
class Employee {
private String name;
private String id;
private int salary;
public Employee(){};
public Employee(String name,String id,int salary){
this.name = name;
this.id = id;
this.salary = salary;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setId(String id){
this.id = id;
}
public String getId(){
return id;
}
public void setSalary(int salary){
this.salary = salary;
}
public int getSalary(){
return salary;
}
public void show(){
System.out.println("员工姓名是" + name + ",工号是" + id + ",薪资是" +salary);
}
}
static 关键字
/*A: static关键字的特点:
*a :随着类的加载而加载;
*b :优先于对象存在;
*c: 被类的所有对象共享
*举例:一个班级的学生应该共用一个班级编号;
*这个特点就告诉我们静态的使用环境是:
*如果某个成员变量是被所有对象共享的,那么它就应该是静态的;
*举例:
饮水机(用静态修饰);
水杯(不能用静态修饰);
*d: 可以通过类名调用
*其实它本身也可以通过对象名调用;
*推荐使用类名调用.
*静态修饰的内容一般我们称其为:与类相关的,类成员
*/
/*
A: static的注意事项
* a:在静态方法中是没有this关键字的;
*静态是随着类的加载而加载,this是随着对象的创建而存在的.
*静态比对象先存在;
* b: 静态方法只能访问静态的成员变量和静态的成员方法;
*静态方法:
*成员变量:只能访问静态变量;
*成员方法:只能访问静态成员方法;
*非静态方法:
*成员变量:可以是静态的,也可以是非静态的;
*成员方法:可以是静态的成员方法,也可以是非静态的成员方法.
*简单记
*静态只能访问静态,
*/
class Demo2_Static {
public static void main(String[] args) {
//Demo d = new Demo();
//d.print1();
Demo.print();
}
}
class Demo {
int num1 = 10;
static int num2 = 20;
public void print1(){ //非静态的成员方法,既可以访问静态的成员也可以访问非静态的成员;
System.out.println(num1);
System.out.println(num2);
}
public static void print(){ //静态的成员方法
//System.out.println(num1); //静态的成员方法不能访问非静态的,错误:无法从静态转换为非静态;
System.out.println(num2);
}
}
静态
静态变量和成员变量的区别
/* 静态变量也叫类变量,成员变量也叫对象变量;
A:所属不同
*静态变量属于类,所以也可以称之为类变量;
*成员变量属于对象,所以也成为实例变量(对象变量);
B:内存中位置不同
*静态变量存储于静态区;
*成员变量存储于堆内存;
C:内存出现时间不同
*静态变量随着类的加载而加载,随着类的消失而消失;
*成员变量随着对象的创建而存在,随着对象的消失而消失
D: 调用不同
*静态变量可以通过类名使用,也可以通过对象使用;
*成员变量值能通过对象名使用
*/
main方法的格式详解
class Demo3_Main {
public static void main(String[] args) {
/*
public :被jvm调用,所以权限要大;
static :被jvm调用,不需要创建对象,直接类名,调用即可;
void : 被jvm调用,不需要任何的返回值;
main : 只有这样写才能被jvm识别,main不是关键字;
String[] args : 以前是用来接收键盘录入的
*/
System.out.println(args.length);
for (int i = 0;i < args.length ;i++) {
System.out.println(args[i]);
}
}
}
工具类中使用静态
/*A:制作一个工具类
*ArrayTool;
*1.获取最大值;
*2.数组的遍历;
3.数组的反转;
*/
class ArrayTool {
//如果一个类中所有的方法都是静态的,需要多做一步,私有构造方法,目的是不让其他类创建本类,
//直接用类名. 调用
private ArrayTool(){};
//获取最大值;
public static int getMax(int[] arr){
int max = arr[0];
for (int i = 1;i < arr.length ;i++ ) {
if (arr[i] > max ) {
max = arr[i];
}
}
return max;
}
//数组的遍历;
public static void print (int[] arr){
for (int i = 0;i < arr.length ;i++ ) {
System.out.print(arr[i] + " ");
}
}
//数组的反转;
public static void reverse(int[] arr){
for (int i = 0;i < arr.length/2 ;i++ ) {
int temp = arr[i];
arr[i] = arr[arr.length-i-1];
arr[arr.length-i-1] = temp;
}
}
}
class Demo_ArrayTool {
public static void main(String[] args) {
int[] arr = {33,11,22,55,44};
/*ArrayTool at = new ArrayTool();
int max = at.getMax(arr); //获取最值
System.out.println(max);
System.out.println("------------");
at.print(arr); //遍历
System.out.println();
System.out.println("------------");
System.out.println("反转后");
at.reverse(arr);
at.print(arr);
*/
ArrayTool.print(arr);
}
}
说明书的制作过程
A:对工具类加入文档注释;
B:通过javadoc命令生成说明书;
*@author(提取作者内容)
*@version(提取版本内容)
javadoc -d 指定的文件目录 -author -version ArrayTool,java
*@param 参数名称//形式参数的变量名称;
*@return 函数运行完返回的数据
/**
这是一个数组工具类,里面封装了查找数组最大值,打印数组,数组反转的方法;
@author ningyanhui
@version v1.0
*/
public class ArrayTool {
//如果一个类中所有的方法都是静态的,需要多做一步,私有构造方法,目的是不让其他类创建本类,
//直接用类名. 调用
/**
私有构造方法
*/
private ArrayTool(){};
//获取最大值;
/**
这是获取数组中最大值的方法
@param arr 接受一个int类型数组
@return 返回数组中的最大值
*/
public static int getMax(int[] arr){
int max = arr[0];
for (int i = 1;i < arr.length ;i++ ) {
if (arr[i] > max ) {
max = arr[i];
}
}
return max;
}
//数组的遍历;
/**
这是遍历数组的的方法
@param arr 接受一个int类型数组
*/
public static void print (int[] arr){
for (int i = 0;i < arr.length ;i++ ) {
System.out.print(arr[i] + " ");
}
}
//数组的反转;
/**
这是反转数组的方法
@param arr 接受一个int类型数组
*/
public static void reverse(int[] arr){
for (int i = 0;i < arr.length/2 ;i++ ) {
int temp = arr[i];
arr[i] = arr[arr.length-i-1];
arr[arr.length-i-1] = temp;
}
}
}
//javadoc -d api -version -author ArrayTool.java
JDK帮助文档的使用
Math类生成随机数功能的简单了解
class Math_Random {
public static void main(String[] args) {
//double d = Math.random();
//System.out.println(d);
//Math.random()会生成大于等于0.0并且小于1.0的伪随机数
for (int i = 0;i < 10 ;i++ ) {
System.out.println(Math.random()) ;
}
//生成1-100的随机数
//Math.random() 0.0000000-0.9999999
//Math.random() * 100 =============>0.00000-99.999999
//(int)(Math.random()*100)=========>0-99
//(int)(Math.random()*100) + 1
for (int i = 0;i < 10 ;i++ ) {
System.out.println((int)(Math.random() * 100) + 1) ;
}
}
}
案例演示
/*
案例演示:
需求:猜数字小游戏(数据在0-100之间)
*/
import java.util.Scanner;
class Test_GuessNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个1-100的整数"); //心里想的数
int guessNum = (int)(Math.random()*100) + 1;
while(true){ //因为需要猜很多次,所以用无限循环
int result = sc.nextInt(); // 猜的数
if (result > guessNum) { //猜的数比心里想的数大
System.out.println("大了");
}else if (result < guessNum) {
System.out.println("小了"); //猜的数比心里想的数小
}else{
System.out.println("中了");
break;
}
}
}
}