对象的创建
Created|Updated|programming languages
|Post Views:
前言
java中的一起都可以看作对象,包括字符串也是一个对象。
一般我们用new操作符创建一个对象。而我们知道字符串对象有两种定义方法。
String str = "xxx";String str = new String("xxx");
- 为什么第一种可以直接赋值呢?
- new新建一个对象有什么含义呢?
了解
- 构造方法,是一个与类同名的方法,用来创建对象。(有参数,或者没参数,有则是初始化)
说明
对象源于类
于下图,Test就表示一个类,类声明一个test对象。等于构造方法。
而也解释了字符串赋值的原因。
构造方法
1 | public book(){ |
- 构造方法中可以为成员变量赋值,这样可以在实例化对象时,初始化成员变量。、
- 如果没有明确构造方法,则编译器将自动创建无参数的构造方法。
Related Articles
2020-03-26
java面向对象思想(this,return)
前言面向对象的思想在主方法中,主要实现: 建对象 给参数 要什么方法就调用 了解 类的成员变量:定义在方法外可以被类方法访问的是成员变量。按照java的编码规范,成员变量一般被定义成private权限。 类的属性:类的成员变量中,外界可以通过set方法和get方法对成员变量进行操作,那么这个成员变量就是类的属性。 this.这个关键字主要用于引用本类中的成员变量或者方法将局部变量的值传递给成员变量给成员变量是为了给本类中的方法使用由于有时类中的属性名和方法中的参数名有时相同,所以目的为了区分类的属性和参数两者. 12345Public Class Student { String name; //定义一个成员变量name private void SetName(String name){ //定义一个参数(局部变量)name this.name = name; //将局部变量的值传递给成员变量} 实例123456789101112131415161718192021package 矩形面积;public class...
2020-12-13
java syntax
Basic conception First, every line of the code must be in the class. Second, a java file can have more than one class. However, there can only be one public class per java file and the name of public class must be the same name as its file name. VariablesA class can contain any of the following variable types. Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the...
2020-11-14
万年历
输入任何一年,2020则输出该年的所有月历表 输入任何年月,2020 11,则输出该月的月历表 输入任何年月日,2020 11 14,则输出该天是星期几 已知年月日,求星期几 int w=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7; 判断是闰年 (year%400 == 0 ) || (year%4==0 && year%100 != 0) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110import java.util.Scanner;public class Demo...
2020-03-25
反转排序算法
目的将一个数组反向排序 主要思想 将数组除以2,因为舍弃小数的缘故,所以适合将前面一半与后面一半调换。 示例1234567891011121314151617181920package 反转排序;public class 反转排序 { public static void main(String[] args) { int arr[] = new int[] {1, 2, 3, 4, 5}; int len = arr.length; int temp; for (int i = 0; i <= len / 2; i++) {//只需要除2 temp = arr[i]; arr[i] = arr[len - i - 1]; arr[len - i - 1] = temp; } for (int a : arr) System.out.print(a + " "); }} 注意 数组的下标从0开始,到length - 1结束
2020-03-29
上下转型
前言 为什么要向上转型?我们直接用继承父类的子类实例化子类对象不就好了吗?这不就可以既能访问子类的对象,也能访问父类对象了吗?如果我们要用被覆盖的父类的方法或者属性,那么用super调用不就好了吗? 介绍向上转型 就是将子类对象赋值给父类类型的变量。 向上转型是为了用父类变量来接受不同的子类对象,调用方法的时候传参父类对象,可以调用子类里不同的重写方法,得到不同的结果。 1Person tom = new Student(); 应用父类12345678910111213public class Animal { public void sleep() { System.out.println("小动物在睡觉"); } public static void doSleep(Animal animal) { // 此时的参数是父类对象,但是实际调用时传递的是子类对象,就是向上转型。 animal.sleep(); } public...
2020-03-29
多态
介绍多态就是一种事物的多种形态。 存在的条件 继承 重写 向上转型(父类引用指向子类对象) 实例123456789101112131415161718192021222324252627282930313233343536373839class Animal{ public Animal(String kinds) {//构造方法 System.out.println("创建一个" + kinds); } void move() { //父类的方法 }}class Fish extends Animal{ public Fish(String kinds) {//子类的构造方法 super(kinds); } public void move() { //改写父类的方法 System.out.println("swim"); }}class Dog extends...
