public class User {
//私有属性
private Long userId;
private String name;
private Integer age;
// 构造方法
public User() {
}
//有残构造方法
public User(Long userId, String name, Integer age) {
this.userId = userId;
this.name = name;
this.age = age;
}
//普通方法
public void say() {
System.out.println("hello world");
}
// 对外包装属性
public String getName() {
return this.name;
}
}
|
// 导入 ArrayList 类
import java.util.ArrayList;
class Test {
public static void main(String[] args) {
ArrayList list = new ArrayList();
}
}
|
// 用到了java.util包目录下的List、ArrayList和LinkedList类
//import java.util.ArrayList;
//import java.util.LinkedList;
//import java.util.List;
//如果不想类上有太多import,就可以直接import 包名.*
import java.util.*;
public class Test {
public static void main(String[] args) {
List list = new ArrayList<>();
List list1 = new LinkedList();
}
}
|
//**精准导入**
//直接导入具体的静态变量、常量、方法方法,注意导入方法直接写方法名不需要括号。
import static com.assignment.test.StaticFieldsClass.staticField;
import static com.assignment.test.StaticFieldsClass.staticFunction;
//或者使用如下形式:
//**按需导入**不必逐一指出静态成员名称的导入方式
//import static com.assignment.test.StaticFieldsClass.*;
public class StaticTest {
public static void main(String[] args) {
//这里直接写静态成员而不需要通过类名调用
System.out.println(staticField);
staticFunction();
}
}
|
请 public class User { //私有属性
private Long userId;
private String name;
private Integer age;
// 构造方法
public User() {
}
//有参构造方法
public User(Long userId, String name, Integer age) {
this.userId = userId;
this.name = name;
this.age = age;
}
//普通方法
public void say() {
System.out.println("hello world");
}
public static void think() {
System.out.println("thinking");
}
// 对外包装属性
public String getName() {
return this.name;
}
|
class Person {
public void say() {
System.out.println("hello");
}
}
public class User extends Person {
public static void main(String[] args) {
Person user = new User();
user.say();
}
}
|
public class Father {
public static void main(String[] args) {
// TODO Auto-generated method stub
Son s = new Son();
s.sayHello();
}
public void sayHello() {
System.out.println("Hello");
}
}
class Son extends Father{
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("hello by ");
}
}
|
public class Father {
public static void main(String[] args) {
// TODO Auto-generated method stub
Father s = new Father();
s.sayHello();
s.sayHello("wintershii");
}
public void sayHello() {
System.out.println("Hello");
}
public void sayHello(String name) {
System.out.println("Hello" + " " + name);
}
}
|
float max(int x,int y);
int max(int x,int y);
// 方法调用
max(1,2);
|
class ExecTest {
public static void main(String[] args) {
Son son = new Son();
}
}
class Parent{
{
System.out.print("1");
}
static{
System.out.print("2");
}
public Parent(){
System.out.print("3");
}
}
class Son extends Parent{
{
System.out.print("4");
}
static{
System.out.print("5");
}
public Son(){
System.out.print("6");
}
}
|
class A {
public int x = 0;
public static int y = 0;
public void m() {
System.out.print("A");
}
}
class B extends A {
public int x = 1;
public static int y = 2;
public void m() {
System.out.print("B");
}
public static void main(String[] args) {
A myClass = new B();
System.out.print(myClass.x);
System.out.print(myClass.y);
myClass.m();
}
}
|
public class CloneTest implements Cloneable {
int num;
public static void main(String[] args) throws CloneNotSupportedException {
CloneTest ct = new CloneTest();
ct.num = 666;
System.out.println(ct.num);
CloneTest ct2 = (CloneTest) ct.clone();
System.out.println(ct2.num);
}
}
|
class CloneTest implements Cloneable {
int num;
public static void main(String[] args) throws CloneNotSupportedException {
CloneTest ct = new CloneTest();
ct.num = 666;
System.out.println(ct.num);
CloneTest ct2 = (CloneTest) ct.clone();
System.out.println(ct2.num);
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class CloneTest2 {
public static void main(String[] args) throws CloneNotSupportedException {
CloneTest ct = new CloneTest();
ct.num = 666;
System.out.println(ct.num);
CloneTest ct2 = (CloneTest) ct.clone();
System.out.println(ct2.num);
}
}
|
热门关键词: 登录