相关疑难解决方法(0)

面向对象范式中的松耦合和紧耦合有什么区别?

在面向对象的范例中,任何人都可以描述松散耦合和紧耦合之间的确切区别吗?

oop coupling object

256
推荐指数
10
解决办法
29万
查看次数

什么是"松散耦合?" 请提供示例

我似乎无法理解"松耦合"的概念.我想这不利于单词"宽松"通常有负面的含义,所以我总是忘了松耦合是一个很好的事情.

有人请展示一些说明这个概念的"之前"和"之后"代码(或伪代码)吗?

language-agnostic oop loose-coupling

168
推荐指数
11
解决办法
11万
查看次数

为什么紧密耦合不好但强烈打字好?

我很难看到松耦合代码的真实好处.为什么花费这么多精力使灵活的东西与各种其他物体一起工作?如果你知道你需要实现什么,为什么不专门为此目的编码?

对我来说,这类似于创建无类型变量:它使它变得非常灵活,但是它可能会导致问题,因为可能传入了一个意外的值.它也使得它更难阅读,因为你没有明确知道传入的是什么.

然而我觉得强烈打字是受鼓励的,但松散耦合是不好的.

编辑:我觉得我对松散耦合的解释是关闭还是其他人正在以错误的方式阅读它.与我的强耦合是当一个类引用另一个类的具体实例时.松散耦合是指类引用另一个类可以实现的接口.

那么我的问题是为什么不专门调用类的具体实例/定义?我将其类比为具体定义您需要的变量类型.我一直在阅读关于依赖注入的一些内容,他们似乎认为松散耦合更好的设计是事实.

strong-typing loose-coupling

9
推荐指数
4
解决办法
4389
查看次数

开始片段活动

我有一个包含许多片段的应用程序,当点击一个按钮时,我试图从一个片段转到另一个片段.

我遇到麻烦的部分是 startActivity(new Intent(HomeFragment.this, FindPeopleFragment.class));

package info.androidhive.slidingmenu;

import info.androidhive.slidingmenu.HomeFragment;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

public class HomeFragment extends Fragment {

public HomeFragment() {
}

ImageButton bSearchByLocation, bSearchByNumber;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);
    View InputFragmentView = inflater.inflate(R.layout.fragment_home,
            container, false);

    bSearchByNumber = ((ImageButton) InputFragmentView
            .findViewById(R.id.bSearchByLocation));
    bSearchByLocation = ((ImageButton) InputFragmentView
            .findViewById(R.id.bSearchByNumber));

    bSearchByLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.bSearchByNumber) …
Run Code Online (Sandbox Code Playgroud)

android

3
推荐指数
1
解决办法
2万
查看次数

热更改片段内父 TextView 的文本?

在我Fragemt.java有这样的事情:

public class MainFragment extends Fragment implements View.OnClickListener {
    private TextView mTitleTextView;
    [...] irrelevant code cut out

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        [...] some other code

        mTitleTextView = (TextView) rootView.findViewById(R.id.titleTextView);
        mTitleTextView.setText("Text I Want to Set"); // Problem! App crashes on start if TextView isn't part of the fragment
        [...] more irrelevant code
Run Code Online (Sandbox Code Playgroud)

这现在工作正常。在应用程序总是crashed加载之后,我搜索了几个小时。问题是TextView (R.id.titleTextView)位于XML父活动的 中,而不是分配给片段的 xml。

有没有办法可以TextView从片段java代码中更改父母的文本?

编辑 logcat …

android android-fragments

0
推荐指数
1
解决办法
820
查看次数