访问另一个非静态类中的方法 [英] Accessing methods in another non-static class

查看:84
本文介绍了访问另一个非静态类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个班级处理数据成员:

保存

记忆

编号



每个人都有Ctor,二传手,吸气剂以及他们需要的各种其他东西。

和数据成员。



我有一个类:

计算器



必须对上述类中存储的变量进行操作。



我不想在Calculator中创建这些类的对象,因为我已经从main()创建了这样的对象!!!!



那么我该如何访问它们?



我尝试将功能更改为静态,

例如:

  public   static   double 添加(){
return Number.getNumber()+ Save.getSave();
}



(在计算器中,这是一个抽象类...)



那个我将如何在抽象类中访问静态方法,但它不会让我,因为它们处理数据成员 - 我甚至无法将方法更改为静态而不会出现错误...



如果我要从类Number创建一个新对象,它将拥有自己的数据成员,而我想访问由创建的对象中的数据成员main()!!!



我可以检索一个引用并以某种方式使用它吗?





感谢您的帮助。

解决方案

请不要使用静态。您应该在那里使用setter创建实例或使用Calculator中的给定实例:



  public   class  MyCalculator  extends 计算器{ //  扩展抽象类 

public MyCalculator(){
this .ignition();
}

private void ignition(){
// 这是有趣的开始

setIntegerValue( 2 ); // 计算器中继承的setter的示例
}



public static void main( String [] args] {
MyCalculator oCalc = new MyCalculator() ; // 打破静态背景
}
}


  public   class  ExampleClass 
{
public string ECName { get ; set ;}
public int ECIndex { get ; set ; }
public string ECDescription { get ; set ; }

public bool checkOtherClassValue(OtherExampleClass oecTemp)
{
if (oecTemp.someValue ==& quot; someValue& quot;)
{
返回 true ;
}
其他
{
返回 ;
}
}
}

public class OtherExampleClass
{
public string someValue {获得; set ; }
public int oecIndex { get ; set ; }
}







然后,在main中,创建两个对象:



  //  创建一个一个类的对象 
OtherExampleClass oecTemp = new OtherExampleClass();
// 设置一些值...
oecTemp.someValue = someValue;

// 创建另一个类的对象
ExampleClass ec = new ExampleClass();
// 将第一个对象作为第二个类中方法的参数传递

bool result = ec.checkOtherClassValue(oecTemp);





您还可以在main中创建一个getter方法,该方法根据请求的索引或名称等返回对象。 / blockquote>

好的,让我们再试一次。



我创建了一个类应用程序

这是我的应用程序 - 令人惊讶,我知道。

请同时考虑一下静态背景。



有一个抽象类 MyAbstractClass 有一些方法。

还有一个类 MyClass 扩展 MyAbstractClass



还有一个函数 parseIntFromString( int),这是抽象类的一部分。我在 MyClass 中更改它,所以它返回我想要的结果(只要函数没有设置为final就可以了。)



借助此扩展程序 MyClass 我可以访问抽象类的所有函数 MyAbstractClass ,正如你在功能中看到的那样 ignition()





  package  com.cp; 

public class 应用程序{

public Application(){
this .ignition();
}

private void ignition(){
MyClass oMyClass = new MyClass();

// 使用MyClass中的方法
System.out。 println( 结果为 + oMyClass.tellIfPositive( 3 ));

// 使用抽象类中的方法通过MyClass
系统.out.println( 结果为 + oMyClass.tellIfTrue( 0 ));

// 使用更改的方法 - abstract将返回0
System.out.println( 结果为 + oMyClass.parseIntFromString( 2));
}

/ * *
* @param args
* /

public static void main( String [] args){
new 应用程序( ); // 打破静态背景
}

// ########################## ###################################
// 扩展抽象类
// 它继承了抽象类的功能,可以有其他功能。
public class MyClass extends MyAbstractClass {

public MyClass(){ / * doAny(); * / }

public boolean tellIfPositive( int iValue){
if 0 < = iValue){ return true; }
return false;
}

// 函数被更改为返回我们需要的结果
@覆盖
public int parseIntFromString( String strValue){
return Integer.parseInt(strValue) ;
}
}

// ###### ################################################## ######
// 带有我们想要使用的方法的抽象类。
public abstract class MyAbstractClass {

// 只是一个简单的功能,没什么大不了的
public boolean tellIfTrue( int iValue){
if 1 == iValue){ return true; }
return false;
}

// 将在MyClass中更改的函数
public int parseIntFromString( String strValue){
return 0 ;
}
}

// ###### ################################################## ########

}


I have 3 classes handling data members:
Save
Memory
Number

each with a Ctor, setters, getters, and various other things they require.
And data members.

I have a class:
Calculator

which has to operate on the variables stored in the aforementioned classes.

I don''t want to create objects from those classes in Calculator, since I already created such objects from main()!!!!

So how can I access them?

I tried changing the functions to static,
for instance:

public static double Add() {
return Number.getNumber() + Save.getSave();
}


(in Calculator, which is an abstract class...)

That would be how I''d access static methods in abstract classes, but but it wouldn''t let me, since they handle data members - I couldn''t even change the methods to static without getting an error...

If I were to create a new object from, say, class Number, it would have its own data members, whereas I want to access the data members in the object created by main()!!!

Can I retrieve a reference and use it somehow, or something like that?


Thanks for helping.

解决方案

Please do not use static. You should create instances or use the given instances in Calculator by using setters there:

public class MyCalculator extends Calculator{ // extending the abstract class

  public MyCalculator(){
    this.ignition();
  }

  private void ignition(){
    // this is where the fun starts

    setIntegerValue(2); // example for an inherited setter from Calculator
  }



  public static void main(String[] args]{
    MyCalculator oCalc = new MyCalculator(); // break out of static context
  }
}


public class ExampleClass
{
    public string ECName { get; set; }
    public int ECIndex { get; set; }
    public string ECDescription { get; set; }

    public bool checkOtherClassValue(OtherExampleClass oecTemp)
    {
        if (oecTemp.someValue == &quot;someValue&quot;)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

public class OtherExampleClass
{
    public string someValue { get; set; }
    public int oecIndex { get; set; }
}




Then, in main, create both of your objects:

//Create an object of one class
OtherExampleClass oecTemp = new OtherExampleClass();
//set some values...
oecTemp.someValue = "someValue";

//create an object of another class
ExampleClass ec = new ExampleClass();
//pass the first object as a param for the method in the second class

bool result = ec.checkOtherClassValue(oecTemp);



You could also create a getter method in main that returns the object based upon a requested index or name etc.


OK, let''s try again.

I created a class Application.
This is my application - surprising, I know.
Please also regard the break out of the static context.

There is an abstract class MyAbstractClass that has some method.
And there is a class MyClass that extends MyAbstractClass.

There is also a function parseIntFromString(int), that is part of the abstract class. I''m changing it in MyClass, so it returns the result I want (That works as long as a function is not set "final").

With the help of this extension MyClass I can access all the functions of the abstract class MyAbstractClass, as you can see in the function ignition()


package com.cp;

public class Application {
	
	public Application(){
		this.ignition();
	}

	private void ignition() {
		MyClass oMyClass = new MyClass();
		
		// using method from MyClass
		System.out.println("The result is " + oMyClass.tellIfPositive(3) );
		
		// using method from abstract class via MyClass
		System.out.println("The result is " + oMyClass.tellIfTrue(0));
		
		// using altered method - abstract would return 0
		System.out.println("The result is " + oMyClass.parseIntFromString("2"));
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Application(); // break out of static context
	}
	
//	#############################################################
//	Extending the abstract class
//	it inherits the functions of the abstract class and can have additional functions.
	public class MyClass extends MyAbstractClass{
		
		public MyClass(){ /* doAny(); */}
		
		public boolean tellIfPositive(int iValue){
			if(0 <= iValue){ return true; }
			return false;
		}
		
		// function is altered to return result we need
		@Override
		public int parseIntFromString(String strValue){
			return Integer.parseInt(strValue);
		}
	}
	
//	##############################################################	
	// abstract class with a method we want to use.
	public abstract class MyAbstractClass {

		// just a simple function, nothing serious
		public boolean tellIfTrue(int iValue){
			if(1 == iValue){ return true; }
			return false;
		}
		
		// function that is going to be altered in MyClass
		public int parseIntFromString(String strValue){
			return 0;
		}
	}
	
//	################################################################

}


这篇关于访问另一个非静态类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆