面向对象,方法,数组总结


学习每一个新知识,我都会思考3个问题:
1)是什么
2)有什么用
3)怎么用
以下所有都是自我总结,仅代表我个人看法

1.方法

1)方法是什么?
方法就是一个被{}圈起来的功能,类似于假如我们想水,而方法便是水龙头,我们只需要简单的操作,他便会实现我们的需求
2)方法有什么用?
我认为方法的出现使得我们的代码变得更加整洁,不需要每次为了实现同样的功能而去重复的编写一段代码,降低了程序冗余性,而且后期想更改程序的话,直接在方法里面改就可以了,增加了程序的维护性.
3)方法怎么用?
先说一下方法的定义格式
修饰符 返回值 方法名(形式参数){
代码块{}
return;
}
4)举个栗子:
假如我们想要打印一首古诗

class LiBaiDemo{
	
	public static void main(String[] args){
		
		System.out.println("床前明月光");
		System.out.println("疑是地上霜");
		System.out.println("举头望明月");
		System.out.println("低头思故乡");
	}
}

这样打印出来不好看,所以我想加一切隔断.像这样:

class LiBaiDemo{
	
	public static void main(String[] args){
		
		System.out.println("床前明月光");
		System.out.println("-------------------");
		System.out.println("疑是地上霜");
		System.out.println("-------------------");
		System.out.println("举头望明月");
		System.out.println("-------------------");
		System.out.println("低头思故乡");
	}
}

可是这样每次我都得重新输出一遍这个隔断,很烦啊,但是用了方法就不一样了:

class LiBaiDemo{
	
	public static void main(String[] args){
		
		System.out.println("床前明月光");
		HangGang(20,'#');
		System.out.println("疑是地上霜");
		HangGang(10,'$');
		System.out.println("举头望明月");
		HangGang(5,'=');
		System.out.println("低头思故乡");
		HangGang(10,'*');
	}
	public static void HangGang(int count,char c){
		for(int i = 1;i <= count;i++){
			System.out.print(c);
		}
		System.out.println();
	}
}

这样既可以更改每行隔断的长度,还可以更改样式,岂不是很妙,运行结果:
在这里插入图片描述方法还有很多用处,这个只是最基本的,还可以将数组的遍历.排序什么的都放进去,方便的很.

2.一维数组

1)数组是什么?
数组就是一堆同类型元素的集合,比如一堆整数,一堆小数什么的.
2)数组有啥用?
存储元素的集合,就好比你买了各种饮料,他们都是喝的属于同类型,数组就是一个袋子,装饮料的.
3)数组怎么用?
问得好!先说一下数组的定义格式:
数据类型[] 数组名 = new 数据类型[数组长度];
数据类型[] 数组名 = {元素1,元素2,元素3,元素4…};
我常用的就是这俩,其他的定义格式都有,大佬自然懂
4)再举个栗子
数组里面的功能挺多,我就说说怎么遍历和排序,这也是我学到数组以来最引以为傲的.话不多说,上代码:

//数组排序的方法
import java.util.Arrays;
public class ArraySortDemo{
	
	public static void main(String[] args){
		
		int[] arr = {3,65,2,6,5,8,10,4,1};
		//先遍历一遍数组
		for(int a : arr){
			System.out.print(a + " ");
		}
		System.out.println();
		System.out.println("--------------------");
		//方法一:冒泡排序:相邻的两个数字比较大小,互换位置.
		/* for(int j = 1;j <= arr.length - 1;j++){//控制循环次数为arr.length - 1
			for(int i = 0;i < arr.length - 1 - j;i++){//每次都要少比较一个数字,因为每次都有一个数字排好了
				if(arr[i] > arr[i+1]){//异或交换值大法,懂的自然懂,不多bb了
					arr[i] = arr[i] ^ arr[i + 1];
					arr[i + 1] = arr[i] ^ arr[i + 1];
					arr[i] = arr[i] ^ arr[i + 1];
				}
			}
			//每一次排序完成后打印观察数组顺序
			for(int c : arr){
				System.out.print(c + " ");
			}
			System.out.println();
			System.out.println("--------------------");
		} */
		//方法二:选择排序:固定值与其他值依次比较大小,互换位置
		for(int i = 0;i < arr.length - 1;i++){
			for(int j = i + 1;j < arr.length;j++){
				if(arr[i] > arr[j]){
					arr[i] = arr[i] ^ arr[j];
					arr[j] = arr[i] ^ arr[j];
					arr[i] = arr[i] ^ arr[j];
				}
			}
			for(int a : arr){
				System.out.print(a + " ");
			}
			System.out.println();
		}
	/* 	方法三:JDk排序:java.util.Array.sort(数组名);//只能升序
		如果没有导包,需要写成java.util.Arrays.sort(数组名);
		如果在程序class外部导包:import java.util.Arrays;
		那就只需要写Arrays.sort(数组名); */
		/* Arrays.sort(arr);
		for(int a : arr){
			System.out.print(a + " ");
		} */
	}
}

这几种排序我都试过,都可以运行,都可以排序,所以大家放心使用,但请大家用的时候不要忘了,这些是我的头发换的,请珍惜,下面请运行结果上场:
这是选择排序的熬当然,完全可以将这几种排序放到方法里面,但是我懒得放.
下面上遍历的代码:

//遍历数组的三种方法
import java.util.Arrays;
public class ArrayBianLiDemo{
	
	public static void main(String[] args){
		
		int[] arr = {0,1,2,3,4,5,6,7,8,9};
		//方法一:使用for循环遍历
		for(int i = 0;i < arr.length;i++){
			
			System.out.print(arr[i] + " ");
		}
		System.out.println();
		
		//方法二:使用Arrays工具类中toString静态方法
		//需要导包:import java.util.Arrays;
		System.out.println(Arrays.toString(arr));
		
		//方法三:使用foreach语句遍历
		for(int a : arr){
			System.out.print(a + " ");
		}
	}
}

我喜欢用增强for循环的遍历方式,因为代码短,不用导包,nice!
运行结果:
在这里插入图片描述
三个愿望一次性满足熬铁子们,三种结果都出来了.

3.二维数组

1)二维数组是什么?
一维数组储存的是元素,它储存的是一维数组.
2)有什么用?
储存一维数组啊
3)怎么用?
就储存一维数组啊
定义格式:
数据类型[][] 数组名 = new 数据类型[一维数组的个数][每个一维数组储存的元素个数]
4)再再举个栗子
话不多说上代码:

//杨辉三角
import java.util.Scanner;
public class YangHuiTriangle{
	
	public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		System.out.println("请输入行数");
		int a = s.nextInt();
		System.out.println("------------------------------");
		yangHuiTriangle(a);
		yangHuiTriangleExample(a);
	}
	//杨辉直角三角形
	public static void yangHuiTriangle(int a){
		int[][] arr = new int[a][a];
		
		for(int i = 0;i < arr.length;i++){
			for(int j = 0;j <= i;j++){
				if(j == 0 || i == j){
					arr[i][j] = 1;
				}else{
					arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
				}
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();
		}
	}
}

这是杨辉三角,当然还有其他很多的应用,看结果吧:
在这里插入图片描述

4.类

1)类是什么?
就是class,里面有属性啊,方法什么的
2)类有啥用?
java中最基本的单元就它,然后组成一个一个的java程序
3)类怎么用?
想怎么用怎么用,比如一个人类,它的属性有姓名,身高,年龄什么的,行为就是它里面的方法,比如吃饭睡觉谈恋爱
4)在来个栗子

//一个手机类
class PhoneTest{
	
	public static void main(String[] args){
		Phone z = new Phone("一加","猛男粉",3999,256);
		z.getPhone();
		z.call("迪丽热巴");
		z.photo("景甜");
		z.chat("杨幂");
	}
}
class Phone{
	private String brand;
	private String color;
	private int price;
	private int memory;
	public void setBrand(String brand){
		this.brand = brand;
	}
	public void setColor(String color){
		this.color = color;
	}
	public void setPrice(int price){
		this.price = price;
	}
	public void setMemory(int memory){
		this.memory = memory;
	}
	public String getBrand(){
		return brand;
	}
	public String getColor(){
		return color;
	}
	public int getPrice(){
		return price;
	}
	public int getMemory(){
		return memory;
	}
	public void getPhone(){
		System.out.println("品牌:" + brand + "\t" + "颜色:" + color + "\t" + "价格:" + price + "\t" + "内存:" + memory);
	}
	public void call(String name){
		System.out.println(brand + "可以和" + name + "打电话");
	}
	public void photo(String name){
		System.out.println(brand + "可以和" + name + "拍照");
	}
	public void chat(String name){
		System.out.println(brand + "可以和" + name + "聊天");
	}
	public Phone(){
		
	}
	public Phone(String brand,String color,int price,int memory){
		this.brand = brand;
		this.color = color;
		this.price = price;
		this.memory = memory;
	}
}

这里面我写的是一个手机类,然后这个类也是一个标准类的写法,涉及到了面向对象的三大特征之一-------封装,就是将类的属性私有化,对外提供访问的方式,运行结果:

在这里插入图片描述学了类以后我们的代码就更加的简洁了,主函数里面什么奇奇怪怪的东西都没有了,看起来就干净了不少,就像刚刮完胡子的你一样.
这里面还有构造方法,就是一个与类同名的方法,没有返回值,作用就是用来初始化class的属性的一个方法.但是建议如果添加了有参构造方法,记得自己加上一个无参的构造方法,不然一不注意就报错了很烦的.
用类名创建了对象以后,可以直接用对象名.方法名调用方法了,很方便.
顺便说一下匿名对象:

//匿名对象
class NoNameDemo{
	public static void main(String[] args){
		StudentTest student = new StudentTest();
		Student s = new Student();
		s.student();
		student.studentExer(s);
		System.out.println("------------------------");
		new Student().student();
		System.out.println("------------------------");
		new StudentTest().studentExer(new Student());
		
	}
}
class Student{
	public void student(){
		System.out.println("好好学习,天天向上");
	}
}
class StudentTest{
	public void studentExer(Student s){
		s.student();
	}
}

运行结果:
在这里插入图片描述匿名对象是用来调用方法的,只能调用一次,用过就扔,类似于某个安全防护产品,如果需要调用类中的某个方法直接
new 类名().方法名();很方便啊

5.this关键字

1)是什么?
就是一个本类的地址值引用,我个人就是使用的时候将他理解为类
2)有什么用?
可以调用本来的构造方法,并且当局部变量和成员变量名字一样的时候用来区分,加this的就是局部变量
3.怎么用?
我说不清楚,上代码看图把

//一个学生类
class StudentTest{
	
	public static void main(String[] args){
		Student z = new Student("狗蛋",35,"女","渣女大波浪");
		Student s = new Student("铁蛋",40,"男","渣男锡纸烫");
		z.getStudent();
		s.getStudent();
		z.Study();
		s.Study();
		z.eat();
		s.eat();
		z.smoke();
		s.smoke();
		z.playGame();
		s.playGame();
	}
}
class Student{
	private String name;
	private int age;
	private String sex;
	private String hairstyle;
	public void setName(String name){
		this.name = name;
	}
	public void setAge(int age){
		this.age = age;
	}
	public void setSex(String sex){
		this.sex = sex;
	}
	public void setHairstyle(String hairstyle){
		this.hairstyle = hairstyle;
	}
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}
	public String getSex(){
		return sex;
	}
	public String getHairstyle(){
		return hairstyle;
	}
	public void getStudent(){
		System.out.println("姓名:" + name + "\t" + "年龄:" + age + "\t" + "性别:" + sex + "\t" + "发型:" + hairstyle);
	}
	public void Study(){
		System.out.println(name + "在认真的学习java!");
	}
	public void eat(){
		System.out.println(name + "在吃饭");
	}
	public void smoke(){
		System.out.println(name + "在抽烟");
	}
	public void playGame(){
		System.out.println(name + "在打游戏");
	}
	public Student(){
		
	}
	public Student(String name,int age,String sex,String hairstyle){
		this.smoke();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.hairstyle = hairstyle;
	}
}

运行结果
在这里插入图片描述this关键字在Student类中的含参构造方法首行调用了本类的方法,为什么我说我在使用的时候就将它看做是类呢
this.方法名();和类名.方法名();是不是很像啊
还有,this.name = name;这样子的也是,前面的this.name我就把它当做我的这个类的name那就是局部变量了不是吗,当然这是我的个人理解,各位看看笑话就好.

6.代码块

1)是什么?
就是用{}包括起来的代码
2)有什么用?
那得看谁修饰的了,一会看代码
3)怎么用?
看代码
4)上代码

public class StaticDemo{
	
	static{
		System.out.println("test static1");
	}
	static{
		System.out.println("test static2");
	}
	public static void main(String[] args){
		Sun sun = new Sun(9);
		
	}
}
class Father{
	static{
		System.out.println("父亲 静态代码块2");
	}
	static{
		System.out.println("父亲 静态代码块1");
	}
	{
		System.out.println("父亲 初始化块");
	}
	Father(){
		System.out.println("父亲 构造器");
	}
}
class Son extends Father{
	static{
		System.out.println("儿子 静态代码块1");
	}
	{
		System.out.println("儿子 初始化块");
	}
	static{
		System.out.println("儿子 静态代码块2");
	}
	Son(){
		System.out.println("儿子 构造器");
	}
}
class Sun extends Son{
	static{
		System.out.println("孙子 代码块");
	}
	{
		System.out.println("孙子 初始化块");
	}
	Sun(){
		System.out.println("孙子 构造器");
	}
}

是不是看起来很繁琐,这还涉及了面向对象三大特征之一:继承
就是一个类认另一个类做爹,然后当爹的就给儿子财产,这里的财产指的是功能,运行结果:
在这里插入图片描述
看起来很乱其实理解了就不乱了,一步一步来
首先程序要找到测试类,就是有主函数的类,进去以后发现有static修饰的代码块,被static修饰的代码块会随着类的加载而加载,所有首先输出test static1和2,static修饰的代码块优先级属于同级所以顺序就是由上至下,紧接着进入了主函数,然后new了一个对象是sun,然后看代码我们发现Sun继承了Son,Son继承了Father,继承有一个特点就是子类运行的时候要把父类初始化一遍,因为不确定父类是否有功能被子类需要,但是呢,既然要初始化就要加载类,所以static修饰的关键字就最先跑出来了,所以输出依次是静态代码块由爷爷到孙子.然后是父亲的构造代码块和构造方法,下来是子类,在下来是孙子的.

7.final关键字

1)是什么?
就是final
2)有什么用?
修饰基本数据类型时,这个变量值一经赋值无法修改,理解不了就想想你的微信号,只能赋值一次
修饰引用类型时,该数据类型在堆内存地址无法更改,但是里面的值可以修改
修饰方法时
可以继承,无法重写
修饰类时
该类不能被继承
3)怎么用?
将你要修饰的变量,方法,类前面加final即可
4)上代码:

lass Fu{
	public final void show(){
		System.out.println("绝密文件,任何人不能修改!") ;
	}
}
class Zi extends Fu{
	public void show(){
		System.out.println("这是一堆垃圾...");
	}
}

//测试类
class FinalDemo{
	public static void main(String[] args){
		//创建子类对象
		Zi z = new Zi() ;
		z.show() ;
	}
}

运行结果:
在这里插入图片描述事实证明,final修饰的方法没办法重写,在看一个:

//测试final修饰的类能否被继承
final class Fu{
	
}
class Zi extends Fu{
	
}
class FinalDemo5{
	public static void main(String[] args){
		Zi z = new Zi();
	}
}

运行结果:
在这里插入图片描述
不让继承…所以别头铁的去继承final修饰的类
在看一个:

class FinalDemo3{
	public static void main(String[] args){
		//定义变量
		final int num = 10 ;
		System.out.println(num); //10
		num = 50 ;//num已经被final修饰并且已经赋值了
		final int num2 ; //定义一个常量,但是没有初始化
		num2 = 20 ;//初始化了
		System.out.println(num2);
		System.out.println("-------------------------");
		
		s2 = new Student() ; //无法为最终变量s2分配值
		
	}
}

运行结果:
在这里插入图片描述没办法更改值,所以别头铁

8.多态

1)是什么?
面向对象的三大特征之一
2)有什么用?
提高了程序的维护性和扩展性
3)怎么用?
有前提条件:
1)必须有继承关系----->没有继承,谈不上多态!
2)必须存在方法重写 :子类需要复写/覆盖父类中的某个功能
(举例:动物的吃和睡的功能,具体的动物应该具备具体的吃和睡的功能)
3)必须有一个父类引用指向子类对象
Fu fu = new Zi() ; //专业名字:向上转型
4)上栗子:

		
class A {
	public void show() {
		show2();
	}
	public void show2() {
		System.out.println("我");
	}
}
class B extends A {
	

	public void show2() {
		System.out.println("爱");
	}
}
class C extends B {
	public void show() {
		super.show();
	}
	public void show2() {
		System.out.println("你");
	}
}
public class Test {
	public static void main(String[] args) {
		A a = new B();
		a.show();
		
		B b = new C();
		b.show();
	}
}

运行结果:
在这里插入图片描述解释一下:
多态的成员访问特点:
成员变量:编译看左边,运行看左!
成员方法:编译看左边,运行看右(由于子类重写了父类的方法)
构造方法:都是对对象中的数据进行初始化(多态是需要有继承关系,还需要遵循分层初始化!)
说一下我对于这个格式的理解
例如: 父类 对象名 = new 子类名();
我的理解就是你这个对象可以做什么是由父类决定的,但是具体怎么做是由子类决定的,比如说一个公司里面有员工,公司决定你可以做什么,但是具体怎么做是由员工决定的
所以 A a = new B();
就是告诉a你可以做A类中的两个方法,运行以后说你要运行show方法,里面又说运行show2方法,但是是B类中执行,所以输出"爱".
下面也是这个意思,所以输出了"你".
我目前学到这里了,下周在总结.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值