《JAVA开发利器MyEclipse全面详解》学习笔记之常用操作

ctrl+1:修正代码错误
设置统一编码风格:Eindow-Preferences-General-Editors-TextEditors-Spelling,Encoding
显示行号:Eindow-Preferences-General-Editors-TextEditors,勾选Show line numbers
alt+shift+s:弹出Source
ctrl+shift+/:添加多行注释add block comment
ctrl+shift+\:删除多行注释remove block comment
alt+shift+J:文档注释generate element comment
生成代理方法:generate delegate methods
     例:AbstractSubject类:
        public interface AbstractSubject {
void sayHello();
}
           RealSubject类:
public class RealSubject implements AbstractSubject {

public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Hello 大家好,我是真实角色");
}

}
ProxySubject类:
public class ProxySubject implements AbstractSubject {
private RealSubject realSubject;

public void sayHello() {
if (realSubject == null) {
realSubject = new RealSubject();
}
realSubject.sayHello();
}

}
包围方式:选择被包围的内容,右击,选择“Surround With”
更改方法标签:change method signature
抽取方法:extract mehod,alt+shift+M
抽取局部变量:extract localvariable,alt+shift+L
抽取常量:extract constant
内联:inline method,alt+shift+I
anonymous翻译为匿名,nested翻译为嵌入
将匿名内部类(通过父类或实现的接口创建,没名称)转换为嵌入内部类(静态内部类),例:
转换前:
public class ConvertNestedTest {
public static void main(String[] args) {
new Runnable() {
public void run() {
// TODO Auto-generated method stub
System.out.println("将匿名内部类转换为嵌入内部类");
}
};
}
}
转换后:
public class ConvertNestedTest {
private static final class MyThread implements Runnable {
public void run() {
// TODO Auto-generated method stub
System.out.println("将匿名内部类转换为嵌入内部类");
}
}

public static void main(String[] args) {
new MyThread();
}
}
将嵌入内部类转换为普通类:
继上例,将鼠标放在MyThread这一行,单击工具栏“Refactor”,选择“Convert Member Type to Level”,变为两个类(在同一包中):
MyThread类:
final class MyThread implements Runnable {
public void run() {
// TODO Auto-generated method stub
System.out.println("将匿名内部类转换为嵌入内部类");
}
}
ConvertNestedTest类:
public class ConvertNestedTest {
public static void main(String[] args) {
new MyThread();
}
}
将局部变量变为成员变量:Convert Loval Variable to Field

抽取超类:extract superclass
抽取前:
ExtractSuperclassTest类:
public class ExtractSuperclassTest {
public void method1(){
System.out.println("方法一");
}

public void method2(){
System.out.println("方法二");
}
}
抽取后:
ExtractSuperclassTest类:
public class ExtractSuperclassTest extends SuperClass {
@Override
public void method1(){
System.out.println("方法一");
}
}
SuperClass类:
public abstract class SuperClass {

public SuperClass() {
super();
}

public abstract void method1();

public void method2() {
System.out.println("方法二");
}

}
抽取接口:extract interface,跟抽取超類差不多。
尽可能使用超类型:
        超类型:当前操作类的超类类型或接口类型
        示例:继抽取超类示例,在ExtractSuperclassTest 类中添加main方法,修改后如下:
public class ExtractSuperclassTest extends SuperClass {
@Override
public void method1() {
System.out.println("方法一");
}

public static void main(String[] args) {
ExtractSuperclassTest eSuperclassTest = new ExtractSuperclassTest();
eSuperclassTest.method1();
}
}
将鼠标放在ExtractSuperclassTest 所在行,选择工具栏"Refactor"-“Use SuperType Where Possible”
原ExtractSuperclassTest 变为:
public class ExtractSuperclassTest extends SuperClass {
@Override
public void method1() {
System.out.println("方法一");
}

public static void main(String[] args) {
SuperClass eSuperclassTest = new ExtractSuperclassTest();
eSuperclassTest.method1();
}
}
重构中两个非常重要的概念:下推、上拉。
上推:操作对象是父类,可将父类的成员移动到它的直接子类。
例:
下推前:
Person类:
public class Person {
int time = 8;
public void sleep(){
System.out.println("睡觉,时间为"+time+"小时");
}

public void eat(){
System.out.println("吃饭!");
}
}
Student类:
public class Student extends Person {
public void study(){
System.out.println("学习");
}
}
进行下推:将鼠标放在Person类所在行,在工具栏选择“Refactor”--“Push Down”
下推后:
Person类:
public abstract class Person {
public abstract void sleep();

public void eat(){
System.out.println("吃饭!");
}
}
Student类:
public class Student extends Person {
int time = 8;

public void study(){
System.out.println("学习");
}

@Override
public void sleep() {
System.out.println("睡觉,时间为"+time+"小时");
}
}
上拉:与下推相反,将子类成员移到父类中,pull up

引入重构
引入间接方法:目的是为了完善某一方法的功能,而代理是为了完善某一类的功能。
”Refactor“--“Introduce Indirection”
示例:
public class Elevator {
public void work() {
System.out.println("电梯上下运动");
}

public void open() {
System.out.println("电梯开门");
}

public void close() {
System.out.println("电梯关门");
}

public static void main(String[] args) {
Elevator elevator = new Elevator();
elevator.work();
}
}
引入间接方法后:
public class Elevator {
public void work() {
System.out.println("电梯上下运动");
}

public void open() {
System.out.println("电梯开门");
}

public void close() {
System.out.println("电梯关门");
}

public static void main(String[] args) {
Elevator elevator = new Elevator();
Elevator.workAll(elevator);
}

public static void workAll(Elevator elevator) {
//红色部分为手动添加
                elevator.open();
elevator.work();
elevator.close();
}
}
引入工厂:
“Refactor”--“Introduce Factory”
引入前:
UserDAOImpl类:
public class UserDAOImpl {
public UserDAOImpl() {

}
}
UserDAOFactory类:
public class UserDAOFactory {
}
引入后:
UserDAOImpl类:
public class UserDAOImpl {
//访问级别变为private
        UserDAOImpl() {

}
}
UserDAOFactory类:
public class UserDAOFactory {

public static UserDAOImpl createUserDAOImpl() {
return new UserDAOImpl();
}

}
引入参数对象:与引入参数不同,“Refactro”--“Introduce Parameter Object”
示例:
引入前:
public class Soldier {
public void sleep(String grade, String name) {
System.out.println(grade + "" + name + "请示睡觉");
}

public void eat(String grade, String name) {
System.out.println(grade + "" + name + "请示吃饭");
}
}
引入后新增了一个Parameter类:
Soldier类:
public class Soldier {
public void sleep(Parameter parameterObject) {
System.out.println(parameterObject.getGrade() + "" + parameterObject.getName() + "请示睡觉");
}

public void eat(String grade, String name) {
System.out.println(grade + "" + name + "请示吃饭");
}
}
Parameter类:
public class Parameter {
private String grade;
private String name;

public Parameter(String grade, String name) {
this.grade = grade;
this.name = name;
}

public String getGrade() {
return grade;
}

public void setGrade(String grade) {
this.grade = grade;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
引入参数:“Refactro”--“Introduce Parameter”
引入前:
public class Soldier {
public void sleep(Parameter parameterObject) {
System.out.println(parameterObject.getGrade() + "" + parameterObject.getName() + "请示睡觉");
}

public void eat(String grade, String name) {
System.out.println(grade + "" + name + "请示吃饭");
}

public void sport(){
String myGrade = "一等兵";//在MyEclipse进行操作是选中‘“一等兵”’
String myName = "张三";
System.out.println(myGrade+""+myName+"请示运动");
}
}
引入后:
public class Soldier {
public void sleep(Parameter parameterObject) {
System.out.println(parameterObject.getGrade() + "" + parameterObject.getName() + "请示睡觉");
}

public void eat(String grade, String name) {
System.out.println(grade + "" + name + "请示吃饭");
}

public void sport(String grade){
String myGrade = grade;
String myName = "张三";
System.out.println(myGrade+""+myName+"请示运动");
}
}
引入方法封装字段:
与生成getter、setter方法非常类似,区别见示例:
引入前:
public class User {
int id;
String name;
}
“Refactor”-“Encapsulate(翻译为封装) Field”
引入后:
public class User {
private int id;//引入后id字段类型变为private
String name;
void setId(int id) {
this.id = id;
}
int getId() {
return id;
}
}

你可能感兴趣的:(MyEclipse)