区分方法,内部类,构造器
基本知识
1、类(Class)和对象(Object)是面向对象的核心概念。
类是对一类事物的描述,是抽象的、概念上的定义
对象是实际存在的该类事物的每个个体,因而也称为实例(instance)。
2、属 性:对应类中的成员变量
行 为:对应类中的成员方法
3、类的实例化
public class PersonTest {
public static void main(String[] args) {
//创建类的对象=类的实例化
Person p1 = new Person();
//Scanner scanner = new Scanner(System.in);
//调用对象的结构:属性、方法
//调用属性:“对象.属性”
p1.name = "Tom";
p1.isMale = true;
System.out.println(p1.name);
//调用方法:“对象.方法”
p1.eat();
p1.sleep();
p1.talk("Chinese");
Person p2= new Person();
System.out.println(p2.name);//null
System.out.println(p2.isMale);//false
}
}
//1.创建类,设计类的成员
class Person{
//属性,成员变量
String name;
int age = 1;
boolean isMale;
//方法
public void eat(){
System.out.println("人可以吃饭");
}
public void sleep(){
System.out.println("人可以睡觉");
}
public void talk(String language){// language形参
System.out.println("人可以说话,使用的是:" + language);
}
}
类和对象的使用
(面向对象思想落地的实现):
- 1.创建类,设计类的成员 :class Person{属性,方法}
- 2.创建类的对象
- 3.通过“对象.属性”或“对象.方法”调用对象的结构
4.3 对象的创建和使用:内存解析 堆栈
通常所说的栈(Stack),是指虚拟机栈。虚拟机栈用于存储局部变量等。局部变量表存放了编译期可知长度的各种基本数据类型(boolean、byte、char 、 short 、 int 、 float 、 long 、double)、对象引用(reference类型,它不等同于对象本身,是对象在堆内存的首地址)。 方法执行完,自动释放。
- 栈的特点:先进后出
- 堆:存放数组,对象实例(new),属性
- 方法里面的变量是局部变量
4.4 成员变量与局部变量
1、在方法体外,类体内声明的变量称为成员变量。
在方法体内部声明的变量称为局部变量。
局部变量除形参外,没有默认初始化值,测试类时均需显式初始化,形参调用时赋值
4.5方法
public void sleep(int hour){
System.out.println("休息了" + hour + "个小时");
eat();
// sleep(10);
}
public String getName(){
if(age > 18){
return name;
}else{
return "Tom";
}
}
*默认方法的权限修饰符先都使用public
Java规定的4种权限修饰符:private、public、缺省、protected -->封装性再细说
- 方法的使用中,可以调用当前类的属性或方法
特殊的:方法A中又调用了方法A:递归方法。
方法中,不可以定义方法。
4.8 构造器
特征:它具有与类相同的名称
它不声明返回值类型。(与声明为void不同)
不能被static、final、synchronized、abstract、native修饰,不能有
return语句返回值
作用:创建对象,对象初始化
注 意:
Java语言中,每个类都至少有一个构造器
默认构造器的修饰符与所属类的修饰符一致
一旦显式定义了构造器,则系统不再提供默认构造器
一个类可以创建多个重载的构造器
父类的构造器不可被子类继承
复习
:
1.面向对象学习的三条主线:
-
Java类及类的成员:属性、方法、构造器;代码块、内部类
-
面向对象的大特征:封装性、继承性、多态性、(抽象性)
-
其它关键字:this、super、static、final、abstract、interface、package、import等
2、类:抽象的,概念上的内容
对象:实体,对象由类派生出来的
3、类与对象创建与执行步骤
- 创建类
- 类的实例化,创建类的对象new
- 调用对象的结构:“对象.属性”
“对象.方法”
4、类的方法能不能定义方法
5、面向对象与面向过程(理解)
-
面向过程:强调的是功能行为,以函数为最小单位,考虑怎么做。
-
面向对象:强调具备了功能的对象,以类/对象为最小单位,考虑谁来做。
6、局部变量存储在栈空间
堆,new结构(数组、对象),对象的属性也存放堆中
方法区:类的加载信息、常量池、静态域
7、局部变量(在方法内)不能有修饰符 -
一、理解“万事万物皆对象”
1.在Java语言范畴中,我们都将功能、结构等封装到类中,通过类的实例化,来调用具体的功能结构 -
>Scanner,String等
-
>文件:File
-
>网络资源:URL
2.涉及到Java语言与前端Html、后端的数据库交互时,前后端的结构在Java层面交互时,都体现为类、对象。
- 二、内存解析的说明
- 1.引用类型的变量,只可能存储两类值:null 或 地址值(含变量的类型)
匿名对象 - 三、匿名对象的使用
- 1.理解:我们创建的对象,没有显式的赋给一个变量名。即为匿名对象
- 2.特征:匿名对象只能调用一次。
- 3.使用:如下
*/
public class InstanceTest {
public static void main(String[] args) {
Phone p = new Phone();
// p = null;
System.out.println(p);
p.sendEmail();
p.playGame();
//匿名对象的方法一
// new Phone().sendEmail();
// new Phone().playGame();
new Phone().price = 1999;
new Phone().showPrice();//0.0
//**********************************
PhoneMall mall = new PhoneMall();
// mall.show(p);
//匿名对象的方法二:常用的匿名对象的使用
mall.show(new Phone());
}
}
class PhoneMall{
public void show(Phone phone){
phone.sendEmail();
phone.playGame();
}
}
class Phone{
double price;//价格
public void sendEmail(){
System.out.println("发送邮件");
}
public void playGame(){
System.out.println("玩游戏");
}
public void showPrice(){
System.out.println("手机价格为:" + price);
}
}
练习4 冒泡排序
package com.atguigu.exer;
/*
* 4. 对象数组题目:
定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1) 生成随机数:Math.random(),返回值类型double;
2) 四舍五入取整:Math.round(double d),返回值类型long。
*
*
* 此代码是对StudentTest.java的改进:将操作数组的功能封装到方法中。
*
*/
public class StudentTest1 {
public static void main(String[] args) {
//声明Student类型的数组
Student1[] stus = new Student1[20];
for(int i = 0;i < stus.length;i++){
//给数组元素赋值
stus[i] = new Student1();
//给Student对象的属性赋值
stus[i].number = (i + 1);
//年级:[1,6]
stus[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
//成绩:[0,100]
stus[i].score = (int)(Math.random() * (100 - 0 + 1));
}
StudentTest1 test = new StudentTest1();
//遍历学生数组
test.print(stus);
System.out.println("********************");
//问题一:打印出3年级(state值为3)的学生信息。
test.searchState(stus, 3);
System.out.println("********************");
//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
test.sort(stus);
//遍历学生数组
test.print(stus);
}
/**
*
* @Description 遍历Student1[]数组的操作
* @author shkstart
* @date 2019年1月15日下午5:10:19
* @param stus
*/
public void print(Student1[] stus){
for(int i = 0;i <stus.length;i++){
System.out.println(stus[i].info());
}
}
/**
*
* @Description 查找Stduent数组中指定年级的学生信息
* @author shkstart
* @date 2019年1月15日下午5:08:08
* @param stus 要查找的数组
* @param state 要找的年级
*/
public void searchState(Student1[] stus,int state){
for(int i = 0;i <stus.length;i++){
if(stus[i].state == state){
System.out.println(stus[i].info());
}
}
}
/**
*
* @Description 给Student1数组排序
* @author shkstart
* @date 2019年1月15日下午5:09:46
* @param stus
*/
public void sort(Student1[] stus){
for(int i = 0;i < stus.length - 1;i++){
for(int j = 0;j < stus.length - 1 - i;j++){
if(stus[j].score > stus[j + 1].score){
//如果需要换序,交换的是数组的元素:Student对象!!!
Student1 temp = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = temp;
}
}
}
}
}
class Student1{
int number;//学号
int state;//年级
int score;//成绩
//显示学生信息的方法
public String info(){
return "学号:" + number + ",年级:" + state + ",成绩:" + score;
}
}
自定义数组的工具类
package com.atguigu.java;
/*
* 自定义数组的工具类
*
*/
public class ArrayUtil {
// 求数组的最大值
public int getMax(int[] arr) {
int maxValue = arr[0];
for (int i = 1; i < arr.length; i++) {
if (maxValue < arr[i]) {
maxValue = arr[i];
}
}
return maxValue;
}
// 求数组的最小值
public int getMin(int[] arr) {
int minValue = arr[0];
for (int i = 1; i < arr.length; i++) {
if (minValue > arr[i]) {
minValue = arr[i];
}
}
return minValue;
}
// 求数组的总和
public int getSum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
// 求数组的平均值
public int getAvg(int[] arr) {
return getSum(arr) / arr.length;
}
//如下的两个同名方法构成了重载
// 反转数组
public 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;
}
}
public void reverse(String[] arr){
}
// 复制数组
public int[] copy(int[] arr) {
int[] arr1 = new int[arr.length];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = arr[i];
}
return arr1;
}
// 数组排序
public void sort(int[] arr) {
// 冒泡排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// int temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
//错误的:
// swap(arr[j],arr[j + 1]);
//正确的:
swap(arr,j,j + 1);
}
}
}
}
//错误的:交换数组中指定两个位置元素的值
// public void swap(int i,int j){
// int temp = i;
// i = j;
// j = temp;
// }
//正确的:交换数组中指定两个位置元素的值
public void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// 遍历数组
public void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println();
}
// 查找指定元素
public int getIndex(int[] arr, int dest) {
// 线性查找:
for (int i = 0; i < arr.length; i++) {
if (dest == arr[i]) {
return i;
}
}
return -1;//返回一个负数,表示没有找到
}
}