主要介绍面向对向的基本知识,我们学习的java就是面向对象语言,所以理解面向对象非常重要,以后再学习其他的面向对象语言也是同样的道理
/*
1.二维数组定义的格式。
*/
class Array2Demo
{
public static void main(String[] args)
{
// int[] arr = new int[3];
// System.out.println(arr);//[I@1fb8ee3 @左边是实体的类型。 @右边是实体的哈希值。
// int[][] arr = new int[3][2];//创建一个二维数组,该数组中有3个一维数组,每一个一维数组中有2个元素。
// System.out.println(arr);//直接打印二维数组。 [[I@c17164
// System.out.println(arr[0]);//直接打印二维数组中的角标0的一维数组。 [I@1fb8ee3
// System.out.println(arr[0][0]);//直接打印二维数组中的角标0的一维数组中角标为0的元素。 0
// int[][] arr = new int[3][];
// System.out.println(arr);//直接打印二维数组。 [[I@c17164
// System.out.println(arr[0]);//直接打印二维数组中的角标0的一维数组。null
// System.out.println(arr[0][0]);//直接打印二维数组中的角标0的一维数组中角标为0的元素。 NullPointerException
// int[][] arr = new int[3][2];
// System.out.println(arr.length);//打印二维数组的长度。其实就是一维数组的个数。
// System.out.println(arr[1].length);//打印二维数组中角标为1一维数组的长度。
int sum = 0;
int[][] arr = {{3,1,7},{5,8,2,9},{4,1}};
for(int x=0; x<arr.length; x++)
{
for(int y=0; y<arr[x].length; y++)
{
// System.out.print(arr[x][y]+”,”);
sum += arr[x][y];
}
}
System.out.println(“sum=”+sum);
// 甲:30 59 28 17
// 乙;37 60 22 19
// int[] arr = {{30,59,28,17},{37,60,22,19}};
int[][][] arr = new int[3][2][4];
}
}
/*
int[] x,y[];
int[] x;
int[] y[];
a
x = y;
b
x = y[0];
c
x[0] = y[0];
d
x[0] = y[0][0];
e
x[0] = y;
*/
2.开始对面向对象进行解读
/*
用java语言对现实生活中的事物进行描述。
通过类的形式来体现的。
怎么描述呢?
对于事物描述通常只关注两方面。
一个是属性,一个是行为。
只要明确该事物的属性和行为并定义在类中即可。
对象:其实就是该类事物实实在在存在的个体。
类与对象之间的关系?
类:事物的描述。
对象:该类事物的实例。在java中通过new来创建的。
*/
/*
描述小汽车
分析:
1,属性。
轮胎数。
颜色。
2,行为。
运行。
定义类其实就是在定义类中的成员。
成员:成员变量<–>属性,成员函数<–>行为。
成员变量和局部变量的区别:
1,
成员变量定义在类中,整个类中都可以访问。
局部变量定义在函数,语句,局部代码块中,只在所属的区域有效。
2,
成员变量存在于堆内存的对象中。
局部变量存在于栈内存的方法中。
3,
成员变量随着对象的创建而存在,随着对象的消失而消失。
局部变量随着所属区域的执行而存在,随着所属区域的结束而释放。
4,
成员变量都有默认初始化值。
局部变量没有默认初始化值。
*/
class Car
{
int num;
String color;
void run()
{
//int num = 10;
System.out.println(num+”…”+color);
}
}
class CarDemo
{
public static void main(String[] args)
{
//在计算机中创建一个car的实例。通过new关键字。
// Car c = new Car();// c就是一个类类型的引用变量,指向了该类的对象。
// c.num = 4;
// c.color = “red”;
// c.run();//要使用对象中的内容可以通过 对象.成员 的形式来完成调用。
// Car c1 = new Car();
// c1.num = 4;
// c1.color = “red”;
// Car c2 = new Car();
// c2.num = 4;
// c2.color = “red”;
// Car c1 = new Car();
// Car c2 = new Car();
// show(c1);
// show(c2);
/*
匿名对象。没有名字的对象 。
new Car();//匿名对象。其实就是定义对象的简写格式。
Car c = new Car();
c.run();
new Car().run();
1,当对象对方法仅进行一次调用的时候,就可以简化成匿名对象。
new Car().num = 5;
new Car().color = “green”;
new Car().run();
2,匿名对象可以作为实际参数进行传递。
*/
// Car c1 = new Car();
// show(c1);
show(new Car());
}
//汽车改装厂。
public static void show(Car c)//类类型的变量一定指向对象。要不就是null。
{
c.num = 3;
c.color = “black”;
System.out.println(c.num+”…”+c.color);
}
}
3.基本数据类型参数传递
class Demo
{
public static void main(String[] args)
{
int x = 3;
show(x);
System.out.println(“x=”+x);
}
public static void show(int x)
{
x = 4;
}
}
4.引用数据类型参数传递
class Demo
{
int x = 3;
public static void main(String[] args)
{
Demo d = new Demo();
d.x = 9;
show(d);
System.out.println(d.x);
}
public static void show(Demo d)
{
d.x = 4;
}
}
5.person类举例
/*
人:
属性:
年龄。
行为:
说话。
*/
/*
private:私有,是一个权限修饰符。用于修饰成员。
私有的内容只在本类中有效。
注意:私有仅仅是封装的一种体现而已。
*/
class Person
{
private /*私有*/int age;
public void setAge(int a)//setXxx getXxx
{
age = a;
}
public int getAge()
{
return age;
}
/*
public void haha(int a)
{
if(a>0 && a<130)
{
age = a;
speak();
}
else
System.out.println(“错误的数据”);
}
*/
void speak()
{
System.out.println(“age=”+age);
}
}
class PersonDemo
{
public static void main(String[] args)
{
Person p = new Person();
// p.age = -20;
p.haha(-20);
// p.speak();
}
public static void selectSort(int[] arr){}
private static void swap(int[] arr,int a,int b){}
}
有什么问题可以随时评论或者留言!
Latest posts by zchao (see all)
- Auraでアクションボタン作成して画面のチェックボックス項目一括処理 - 2021年4月12日
- デフォルト項目値を含むレコード作成実例説明(defaultFieldValues) - 2021年1月9日
- Salesforce のノーコード・ローコード開発 - 2020年12月31日