###构造对象与static关键字以及Math类产生的随机数
#1、面向对象(构造方法概述和格式)
构造方法概述和作用:
创建对象,给对象中的成员进行初始化
构造方法格式特点:
(1)方法名与类名相同
(2)没有返回值类型 无void
(3)没有具体的返回值
#2、面向对象(构造方法的重载及注意事项)
构造方法时应该注意:如果我们没有给出构造方法
系统将自动提供一个无参数
的构造方法,如果我们给出了
构造方法,系统将不会赋给默认值
建议:自己给出无参数构造方法
给成员变量赋值的两种方式
setXxx()方法
构造方法
案例演示:构造方法的重载
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.westos.demo;
public class MyTest {
public MyTest() {
}
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.setName("刘亦菲");
teacher.setAge(19);
System.out.println(teacher.getName() + "---" + teacher.getAge());
Teacher teacher1 = new Teacher("林青霞", 28);
System.out.println(teacher1.getName() + "===" + teacher1.getAge());
}
}
案例演示:完成一个标准学生类的代码即测试
要求:完善学生类
给成员变量进行赋值
输出成员变量(两种方式)
package com.baiduMydemo6;
import com.sun.org.apache.xpath.internal.operations.String;
import java.security.PublicKey;
/**
* @Author: dell
* @CreateTime: 2018-12-12 11:57
*/
public class student {
private String name;
private int age;
private int grade;
private String colar;
public student(){
}
public void setName(String mingzi){
this. name=mingzi;
}
public String getName(){
return name;
}
public void setGrade(int nianji){
this.grade =nianji;
}
public void setColar(String yanse){
this.colar =yanse;
}
public void setAge(int nianling) {
if (nianling <= 1 || nianling >= 100) {
this. age = nianling;
} else {
System.out.println("你的输入不合理");
;
}
}
public int getAge(){
return age;
}
public int getGrade(){
return grade;
}
public String getColar(){
return colar;
}
}
package com.baiduMydemo6;
/**
* @Author: dell
* @CreateTime: 2018-12-12 12:03
*/
public class test1 {
public static void main(String[] args) {
student student = new student();
student.setAge(17);
student.setGrade(17);
System.out.println(student.getName());
System.out.println(student.getColar());
System.out.println(student.getGrade());
System.out.println(student.getAge());
}
}
#3、案例演示:完成手机类的代码及测试
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.westos.demo3;
public class MyTest {
public MyTest() {
}
public static void main(String[] args) {
Student.num = 100;
Student student = new Student();
System.out.println(Student.num);
System.out.println(Student.num);
Student.show();
Student.show();
}
}
package com.baiduMydemo6;
import javax.naming.Name;
/**
* @Author: dell
* @CreateTime: 2018-12-12 18:17
*/
public class test2 {
public static void main(String[] args) {
phone phone = new phone();
phone.name ="小米";
phone.price =3200;
phone.yanzhi="高";
System.out.println(phone.name);
System.out.println(phone.price);
System.out.println(phone.yanzhi);
phone.dadianhua();
phone.faduanxin();
phone.playgame();
}
}
#4、面向对象(创建一个对象的步骤)
(1):加载Student.class文件进内存
(2):在栈内存为s开辟空间
(3):在堆内存为学生对象开辟空间
(4):对学生对象的成员变量进行默认初始化
(5):对学生对象的成员变量进行显示初始化
(6):通过构造方法对学生对象的成员变量赋值
(7):学生对象初始化完毕,把对象地址赋值给s变量
#5、长方形案例演示:需求:
定义一个长方形(RectangleDemo)类,
定义求周长(length)和面积(area)的方法,
然后定义一个测试类Test,进行测试。
class Rectangle{
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//提供计算周长的方法
public double getGirth(){
//ctrl+shift+enter 补分号
return (width + height) * 2;
}
//提供计算面积的方法
public double getArea(){
return width*height;
}
}
package org.westos.demo2;
/**
* @Author: Administrator
* @CreateTime: 2018-12-09 09:45
*/
public class MyTest {
public static void main(String[] args) {
//A:
//案例演示
//需求:
//定义一个长方形(RectangleDemo) 类, 定义求周长(length) 和面积(area) 的方法,
// 然后定义一个测试类Test,进行测试。
//采用无参构造
Rectangle rectangle = new Rectangle();
rectangle.setHeight(10);
rectangle.setWidth(5);
double area = rectangle.getArea();
double girth = rectangle.getGirth();
System.out.println("面积是"+area+" 周长是 "+girth);
System.out.println("------------------------------");
// 采用有参构造
Rectangle rectangle1 = new Rectangle(10, 10);
double area1 = rectangle1.getArea();
double girth1 = rectangle1.getGirth();
System.out.println("面积是" + area1 + " 周长是 " + girth1);
}
}
#6、员工类案例演示:需求:定义一个员工(Employee)类,自己分析出
几个成员,然后给出成员变量,, 以及
一个显示所有成员信息的方法。并测试。
#7、面向对象(static关键字)
static 关键字的特点:
(1) 随着类的加载而加载
(2)优先于对象的存在
(3)被类的所有对象所共享
(4)可以通过类名进行调用
其本身也可以通过对象名进行调用
static 关键字的注意事项:
(1) 在静态方法中是没有static的关键字的
(2)静态方法只能访问静态成员变量和静态
成员方法
案例演示:static关键字
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.westos.demo3;
public class Student {
public String name;
public static int num;
public Student() {
}
public static void show() {
System.out.println("这是一个静态的show()");
}
public void test() {
System.out.println(this.name);
System.out.println(num);
}
public static void show2() {
System.out.println(num);
}
public void test2() {
show();
show();
this.test();
}
public static void test3() {
show();
show();
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.westos.demo3;
public class MyTest {
public MyTest() {
}
public static void main(String[] args) {
Student.num = 100;
Student student = new Student();
System.out.println(Student.num);
System.out.println(Student.num);
Student.show();
Student.show();
}
}
#8、面向对象(静态变量和成员变量的区别)
A:所属不同
静态变量属于类,所以也称为类变量
成员变量属于对象,所以也称为实例变量(对象变量)
B:内存中位置不同
静态变量存储于方法区的静态区
成员变量存储于堆内存
C:内存出现时间不同
静态变量随着类的加载而加载,随着类的消失而消失
成员变量随着对象的创建而存在,随着对象的消失而消失
D:调用不同
静态变量可以通过类名调用,也可以通过对象调用
成员变量只能通过对象名调用
#9、如何使用JDK提供的帮助文档
A:找到文档,打开文档
B:点击显示,找到索引,出现输入框
C:你应该知道你找谁?举例:Scanner
D:看这个类的结构(需不需要导包)
java.lang包下的内容不需要我们手动导入
其它包下的内容需要我们手动导入
类 API文档
成员变量 字段摘要
构造方法 构造方法摘要
成员方法 方法摘要
E:看这个类的说明(简单的了解一下)
F:看开始版本
G:看构造方法
H:看成员方法
看左边:
是否是static的,如果是我们就不需要创建对象,直接可以使用类名调用该方法;看返回值,返回值是什么我就使用什么接收
看右边:
看参数列表: 参数的个数 , 参数的类型 ; 要什么参数我是用的时候,就传递什么参数
I:然后使用
#10、面向对象(学习Math类的随机数功能)
:Math类概述:
类包含用于执行基本数学运算的方法
Math类特点:
由于Math类在java.lang包下,所以不
需要导包。
没有构造方法,因为它的成员全部是静态的。
获取随机数的方法
public static double random():返回带
正号的 double 值,该值大于等于 0.0 且小于 1.0。
举例;获取一个1-100之间的随机数:
int number = (int)(Math.random()*100)+1;