目录
一、属性(变量)
1、变量的分类:
局部变量和成员变量的相同和不同点:
(1)相同点 :
(2)不同点:
①位置不同:
②权限修饰符不同:
③默认值
④内存的位置不同
案例实操:
public class FieldTest {
public static void main(String[] args) {
int a = 10;
System.out.println(a);
//输出默认值
new Student().value();
}
}
class Student{
// int a = 10;
byte a;
short b;
int c;
long d;
float e;
double f;
char g;
boolean boo;
public void value(){
System.out.println(a); //10
System.out.println(b); //0
System.out.println(c); //0
System.out.println(d); //0
System.out.println(e); //0
System.out.println(f); //0.0
System.out.println(g); //0.0
System.out.println(boo); //false
}
/*
* 代码块
*/
{
int d = 20;
}
/*
* 默认无返回值方法
*/
public Student(int a){
int b = 10;
}
public Student(){
}
/*
* 声明无返回值方法
*/
public void show(int a,int b){
int c = 10;
}
}
二、方法
1、例子:
public void show(){} //无参无返回值
public int getAge(){return 10;} //无参有返回值
public void say(String str){} //有参无返回值
public String getName(String name){return "aa";} //有参有返回值
2、格式:
权限修饰符 返回值类型 方法名(形参列表){
方法体;
}
3、方法的说明:
1.权限修饰符 :四种权限修饰符 ,public protected 缺省的 private (现阶段都写成public)
2.返回值类型 :void / 具体的类型
void : 无返回值
具体的类型 : 基本数据类型 、引用数据类型。如果有返回值类型,
那么方法体中应该 有返回值 "return 具体的类型"
3.方法名:遵守标识符的规则和规范即可 (一定要做到见名知义)
4.形参列表 : 形参列表中的变量可以是0个1个或多个。多个变量之间用","分开
5.方法体:调用方法时才会执行方法体。
4、return 关键字: 在方法中使用
5、方法的重载
方法的重载的概念:
同一个类中相同的方法名,不同的形参列表构成重载
举例:
public void add(int a,int b){
System.out.println("int,int");
System.out.println(a + b);
}
//需求?求两处double类型变量的和
public void add(double a,double b){
System.out.println("double,double");
System.out.println(a + b);
}
如何确定类中某一个方法的调用:方法名 + 形参列表
说明:
6、值传递
形参:方法声明时的参数
实参:方法调用时实际传给形参的参数值
总结:
①基本数据类型: 如果传递的数据是基本数据类型 ,实际上传递的是该变量中的具体的值。
②引用数据类型:如果传递的数据是引用数据类型,实际上传递的是该对象的地址值。
三、构造器
1、作用:
①创建对象
②用来给对象进行初始化
2、格式:
权限修饰符 类名(形参列表){
方法体;
}
3、说明:
4、属性的赋值方式有几种?
四 、案例实操
案例(1) 方法与返回值
public class MethodTest {
public static void main(String[] args) {
Animal animal = new Animal();
animal.show();
int a = animal.getAge();
System.out.println(a);
//打印出来的就是方法的返回值
System.out.println(animal.getAge());
//编译错误 ,因为方法没有返回值
// System.out.println(animal.show());
animal.say("aaaaa");
String str = "bbbb";
animal.say(str);
animal.add(1,2);
//调用方法时传递的实参的类型和个数必须和形参保持一致
animal.info("小苍", 16);
animal.info2();
}
}
/*
* 同一个包下的类名不能相同 : The type Person is already defined
*/
class Animal{
String name;
int age;
//形参列表的作用就是提醒方法的调用者给传输一些需要的数据
public void say(String str){
System.out.println(str + name + age);
}
public void add(int a,int b){
System.out.println(a + b);
}
public void info(String name,int age){
System.out.println(name + " " + age);
}
public void info2(){
show(); //在同一个类中方法之间可以互相调用,在同一个类中方法也可以调用属性
// info2(); 不要自己调用自己会出异常
}
/*
* return 关键字:
* 如果方法有返回值,那么"return 具体的数值类型"用来给方法的调用者返回数据,同时结束方法。
* 如果方法没有返回值,那么"return"用来结束当前方法
*/
//无参无返回值的
public void show(){
System.out.println("say");
for(int i = 0; i <= 5; i++){
if(i == 2){
// break;
return;
}
System.out.println(i);
}
//return; //如果没有返回值,那么return用来结束当前方法
System.out.println("方法执行完毕");
}
//无参有返回值的
public int getAge(){
// return 10;
int a = 20;
return a; //return 具体的数值类型。用来返回给方法的调用者一个数据
}
}
say
0
1
20
20
aaaaanull0
bbbbnull0
3
小苍 16
say
0
1
案例(2)有返回值、无返回值
3.1 编写程序,声明一个method方法,在方法中打印一个10*8 的矩形,在main方法中调用该方法。
3.2 修改上一个程序,在method方法中,除打印一个10*8的矩形外,再计算该矩形的面积,并将其作为方法返回值。
在main方法中调用该方法,接收返回的面积值并打印。
3.3 修改上一个程序,在method方法提供m和n两个参数,方法中打印一个m*n的矩形,并计算该矩形的面积,
将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
public class MethodTest {
public static void main(String[] args) {
MethodTest mt = new MethodTest();
System.out.println("area = " + mt.method(5,5));
System.out.println("area = " + mt.method(10,8));
}
/*
public void method(){
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 10; j++) {
System.out.print("*");
}
System.out.println();
}
}
*/
/*
public int method(){
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 10; j++) {
System.out.print("*");
}
System.out.println();
}
return 10 * 8;
}
*/
public int method(int m,int n){
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
System.out.print("*");
}
System.out.println();
}
return m * n;
}
}
案例(3) 重载
1.编写程序,定义三个重载方法并调用。方法名为mOL。
三个方法分别接收一个int参数、两个int参数、一个字符串参数。分别执行平方运算并输出结果,相乘并输出结果,输出字符串信息。
在主类的main ()方法中分别用参数区别调用三个方法。
2.定义三个重载方法max(),第一个方法求两个int值中的最大值,第二个方法求两个double值中的最大值,
第三个方法求三个double值中的最大值,并分别调用三个方法。
/*
* JavaBeen
* 1.类是公共的
2.有一个无参的公共的构造器
3.有属性,且有对应的get、set方法
*/
public class Demo {
public static void main(String[] args) {
Demo demo = new Demo();
demo.mOL(10);
demo.mOL(5, 10);
demo.mOL("我是龙哥我最大");
System.out.println("--------------------");
demo.max(10.5, 12.3);
demo.max(5, 10);
demo.max(1.5, 2.3,3.8);
}
public void max(int a, int b) {
int max = (a > b)? a : b;
System.out.println(max);
}
public void max(double a, double b) {
double max = (a > b)? a : b;
System.out.println(max);
}
public void max(double a, double b,double c) {
double max = (a > b)? a : b;
max = (max > c)? max : c;
System.out.println(max);
}
public void mOL(int a) {
System.out.println(a * a);
}
public void mOL(int a, int b) {
System.out.println(a * b);
}
public void mOL(String str) {
System.out.println(str);
}
}
案例(4) 值传递
public class ValueTra {
public static void main(String[] args) {
ValueTra vt = new ValueTra();
int a = 5;
int b = 10;
//数据交换前的值
System.out.println("a=" + a + " b=" + b);
//调用方法实现数据的交换
vt.swap(a, b);
//数据交换后的值
System.out.println("a=" + a + " b=" + b);
System.out.println("-------------------引用类型的值传递-----------------");
Value v = new Value();
v.a = 10;
v.b = 5;
//数据交换前的值
System.out.println("v.a=" + v.a + " v.b=" + v.b);
vt.swap(v);
//数据交换后的值
System.out.println("v.a=" + v.a + " v.b=" + v.b);
}
public void swap(Value v){
int temp = v.a;
v.a = v.b;
v.b = temp;
}
public void swap(int a,int b){
System.out.println("swap前 a=" + a + " b=" + b);
int temp = a;
a = b;
b = temp;
System.out.println("swap后 a=" + a + " b=" + b);
}
}
class Value{
int a;
int b;
}
打印结果:
a=5 b=10
swap前 a=5 b=10
swap后 a=10 b=5
a=5 b=10
-------------------引用类型的值传递-----------------
v.a=10 v.b=5
v.a=5 v.b=10
案例(5)JavaBean
public class JavaBeen {
private String name;
private int age;
public JavaBeen(){
}
public JavaBeen(String n,int a){
name = n;
age = a;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void say(){
}
}
案例(6) 构造器
public class AnimalTest {
public static void main(String[] args) {
//通过构造器赋值,只能赋值一次。
/*
Animal animal = new Animal("强哥",38);
animal.show();
Animal animal2 = new Animal();
animal2.setName("强强哥");
animal2.setAge(48);
animal.show();
*/
System.out.println("----------------验证属性赋值的顺序-------------");
/*
Animal animal = new Animal(20);
animal.setAge(60);
animal.show(); //显示赋值和构造器赋值。
*/
Animal animal = new Animal();
System.out.println(animal.getAge());
}
}
class Animal{
private String name;
private int age = 4;
//构造器 (构造方法):
public Animal(){
System.out.println("我是构造器");
}
public Animal(int a){
age = a;
System.out.println("我是构造器" + a);
}
public Animal(String n,int a){
name = n;
age = a;
//作一些其它方法的初始化的操作
System.out.println("---------------");
System.out.println("哇哇哇..................");
}
public void show(){
System.out.println(name + " " + age);
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
public void setAge(int a){
age = a;
}
public int getAge(){
return age;
}
}
案例(7) 权限修饰符
public class Person{
private String namePrivate = "namePrivate";
String name = "name";
public String namePublic = "namePublic";
public void sayPublic(){
System.out.println("sayPublic");
}
void say(){
System.out.println("say");
}
private void sayPrivate(){
System.out.println("sayPrivate");
}
/*
* 在类的内部所有的权限修饰都可以进行调用
*/
public void show(){
sayPublic();
sayPrivate();
say();
System.out.println(name);
System.out.println(namePrivate);
System.out.println(namePublic);
}
}
main 方法:
public class PersonTest {
public static void main(String[] args) {
Person person = new Person();
//在其它的类中进行调用(同一个包中)
System.out.println(person.name);
System.out.println(person.namePublic);
/*
* 本包中可以调用 - public 缺省的 protected(后面再讲)
*
* 在A类中不能调用 B类中 - private
*/
person.sayPublic();
person.say();
}
}